Android Internal Storage Example Tutorial

Today we will look into android internal storage. Android offers a few structured ways to store data. These include

In this tutorial we are going to look into the saving and reading data into files using Android Internal Storage.

Android Internal Storage

Android Internal storage is the storage of the private data on the device memory. By default, saving and loading files to the internal storage are private to the application and other applications will not have access to these files. When the user uninstalls the applications the internal stored files associated with the application are also removed. However, note that some users root their Android phones, gaining superuser access. These users will be able to read and write whatever files they wish.

Reading and Writing Text File in Android Internal Storage

Android offers openFileInput and openFileOutput from the Java I/O classes to modify reading and writing streams from and to local files.

  • openFileOutput(): This method is used to create and save a file. Its syntax is given below:

    The method openFileOutput() returns an instance of FileOutputStream. After that we can call write method to write data on the file. Its syntax is given below:
  • openFileInput(): This method is used to open a file and read it. It returns an instance of FileInputStream. Its syntax is given below:

    After that, we call read method to read one character at a time from the file and then print it. Its syntax is given below:

    In the above code, string temp contains all the data of the file.
  • Note that these methods do not accept file paths (e.g. path/to/file.txt), they just take simple file names.

Android Internal Storage Project Structure

project-structure2-233x450

Android Internal Storage Example Code

The xml layout contains an EditText to write data to the file and a Write Button and Read Button. Note that the onClick methods are defined in the xml file only as shown below:

activity_main.xml

The MainActivity contains the implementation of the reading and writing to files as it was explained above.

Here, a toast is displayed when data is successfully written into the internal storage and the data is displayed in the EditText itself on reading the data from the file.

The image shown below is the output of the project. The image depicts text being written to the internal storage and on clicking Read it displays back the text in the same EditText.

android internal storage, android file storage, Android OpenFileOutput

Where is the file located?

To actually view the file open the Android Device Monitor from Tools->Android->Android Device Monitor.
The file is present in the folder data->data->{package name}->files as shown in the images below:

project-structure2-233x450

The file “mytextfile.txt” is found in the package name of the project i.e. com.journaldev.internalstorage as shown below:

android-sqlite-db-1-450x218
Download the final project for android internal storage example from below link.

By admin

Leave a Reply