Cache
If an application needs to cache data, it is prudent to use a cache storage mechanism provided by the Android stack. Android stores cache files in the filesystem along with the application so that they are sandboxed with the application that created it. All cache files are created in the /data/data/<application-path>/cache/
directory. In case the system is running low on memory, these cache files are deleted first. Regular pruning of these files is necessary as they may grow big and eat up disk space.
The following code snippet first writes a string to the cache file and then reads the same string from the cache file. As you will notice, reading and writing is the same as any file input/output, only the location of the file is obtained using getCacheDir()
to write a string.
//Write to the cache file String myString = new String ("Hello World!"); File file = new File (getCacheDir(), "MyCacheFile"); FileOutputStream fOut = new FileOutputStream(file); OutputStreamWriter osw = new...