Saving and reading files in the internal storage

You can save and read files in the internal storage as private files which only your app can see except for other apps with root privileges.
The absolute path of this folder is data\data\[your app package]\files
The class Context provides some methods which help you to make operations such as:

  • openFileInput(String name) Open a private file associated with this Context’s application package for reading
  • openFileOutput(String name, int mode) Open a private file associated with this Context’s application package for writing
  • getFilesDir() Gets the absolute path to the filesystem directory where your internal files are saved
  • getDir() Creates (or opens an existing) directory within your internal storage space
  • deleteFile() Deletes a file saved on the internal storage
  • fileList() Returns an array of files currently saved by your application

and you don’t need to have special permissions in order to use these methods.

For example the following code taken from Using the Internal Storage creates a file containing the string “hello world!”

try {
	String FILENAME = "hello_file";
	String string = "hello world!";

	FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);

	fos.write(string.getBytes());
	fos.close();

} catch (IOException e) {
	Log.e("ERROR", e.toString());
}

where at the line 5, in place of MODE_PRIVATE you can use MODE_APPEND, other constants such as MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE are deprecated.

The following code reads the same file and shows a message with the text “hello world!”

try {
	String FILENAME = "hello_file";
	byte[] bytes = new byte[1024];

	FileInputStream fis = openFileInput(FILENAME);

	fis.read(bytes);
	fis.close();

	String string = new String(bytes);
	Toast.makeText(getApplicationContext(), string, Toast.LENGTH_SHORT).show();

} catch (IOException e) {
	Log.e("ERROR", e.toString());
}

But these examples have a limitation: they can’t be used in sub folders, in other words you can’t create a tree structure of directories and files.
If you need to create files in subdirectories you must use the standard technique of java, for example the following code creates a file inside the folder “sub”

try {

	String FILENAME = "hello_file";
	String FOLDERNAME = "sub";
	String string = "hello world!";

	Context context = getApplicationContext();
	String folder = context.getFilesDir().getAbsolutePath() + File.separator + FOLDERNAME;

	File subFolder = new File(folder);

	if (!subFolder.exists()) {
		subFolder.mkdirs();
	}

	FileOutputStream outputStream = new FileOutputStream(new File(subFolder, FILENAME));

	outputStream.write(string.getBytes());
	outputStream.close();

} catch (FileNotFoundException e) {
	Log.e("ERROR", e.toString());
} catch (IOException e) {
	Log.e("ERROR", e.toString());
}

and the following code reads the same file

try {

	String FILENAME = "hello_file";
	String FOLDERNAME = "sub";
	byte[] bytes = new byte[1024];

	Context context = getApplicationContext();
	String folder = context.getFilesDir().getAbsolutePath() + File.separator + FOLDERNAME;

	File subFolder = new File(folder);

	FileInputStream outputStream = new FileInputStream(new File(subFolder, FILENAME));

	outputStream.read(bytes);
	outputStream.close();

	String string = new String(bytes);
	Toast.makeText(getApplicationContext(), string, Toast.LENGTH_SHORT).show();

} catch (FileNotFoundException e) {
	Log.e("ERROR", e.toString());
} catch (IOException e) {
	Log.e(TAG, e.toString());
}

Comments

5 responses to “Saving and reading files in the internal storage”

  1. Timothy Lubinga Avatar
    Timothy Lubinga

    very informative, thanks a lot.

  2. Hi, I have a question that is my homework,
    Where are the files saved when the user does not specify the route?

    1. data/data/[your package]/files

  3. I don’t understand why I keep getting the same message: “error: cannot find symbol method getApplicationContext()” no matter what examples I choose. What do I need to eliminate it? Thank you for your answer!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.