Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a cache file previously written by the same app

I'm writing to a file using the code below:

File file = new File(getCacheDir(), "cachefile");
FileOutputStream fos = new FileOutputStream(file);
StringBuilder cachetext = new StringBuilder();
Iterator bri = brands.iterator();
Iterator bli = brand_id.iterator();
while(bli.hasNext()) {
    cachetext.append(bli.next() + "|" + bri.next() + System.getProperty("line.separator"));
}
fos.write(cachetext.toString().getBytes());
fos.close();

This works fine - no errors and the file ends up containing what I expect it to contain. When I go to read it via openFileInput(), however, I get an exception telling me that path separators are not allowed

FileInputStream fis = openFileInput(getCacheDir() + "/cachefile");

Fair enough, that contains a slash, but how else can I specify the path of the file I want to open? There must be a way to do this, of course, but I can't find answers via Google ('read', 'cache' and 'file' not being the most niche of terms ...) so I thought I'd try the human touch. Thanks in advance!

like image 213
Richard Avatar asked Feb 08 '11 13:02

Richard


People also ask

How do I view Android application specific cache?

On Android Studio you can use Device File Explorer to view /data/data/your_app_package/cache. Click View > Tool Windows > Device File Explorer or click the Device File Explorer button in the tool window bar to open the Device File Explorer. Save this answer. Show activity on this post.

How do I view app cache?

Open Settings and select Storage. In the resulting list, tap the Apps entry (Other Apps on Android 11 and earlier). This will take you to a list of all the apps installed on your phone. Choose the app whose cache you want to clear.

How do you retrieve data from cache?

You can fetch a custom object from the cache using various overloads of the Get() method by specifying the key of the cache item. The object is retrieved as a template, so it needs to be type-cast accordingly if it is a custom class object.

Can you view cache files?

The program that created the CACHE file is the only software that can use it. To open a CACHE file to see it in its text form, just use a regular text editor like Windows Notepad or one of these free text editors.


1 Answers

you do it pretty much the same way you created the output file:

FileInputStream fis = new FileInputStream(new File(getCacheDir(), "cachefile"));
like image 168
James Dunham Avatar answered Nov 03 '22 09:11

James Dunham