January 2013
Intermediate to advanced
408 pages
9h 23m
English
Files stored in the files folder are persistent; they will always be there until you explicitly delete them. Sometimes, however, you may not want this to happen because you only need to save the files temporarily and do not need them again later. For this purpose, you should save the files into the cache folder.
To save files to the cache directory, use the getCacheDir() method to return the absolute path to the application-specific cache directory on the filesystem. You can then use the File class to create a full path for the file that you want to store in the cache folder.
The following code snippet shows how to save a file into the cache folder and then read back its content:
package net.learn2develop.cache;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast; public class MainActivity extends Activity { static final int READ_BLOCK_SIZE ...Read now
Unlock full access