Android: Storing Data and Accessing Internal Storage Tutorial

There are several ways to store and access data on Android. They are:

In this tutorial, I will demonstrate how to create and access a file from internal storage. Internal storage is the storage of the private data in the device memory.

Writing to a file

In order to open a file for writing, we call the method openFileOutput (String fileName, int mode). This method creates the file if the specified file does not exist. Below is the code snippet used to write to a file:

In the above code snippet, we use file open mode as Context.MODE_PRIVATE, so that the file can only be accessed by the calling application. message contains the text to be written to the file.

Reading a file

To read the file first we open the file using openFileInput(String fileName). Then we iterate over the fileInput, till the End of File (EOF) is reached. When the EOF is reached, read() method returns -1.

The User Interface

Let’s create a text box: to input and retrieve file data and 2 buttons: 1 to read from the file and other to write to file.

The Complete Class

We will store the data in storagefile.txt. When the user clicks on write button, the contents of the EditText will be written into the internal storage. On the successful write to the internal storage, we clear the EditText so that, when the user clicks on the read button, the contents can be populated in the EditText.

If you have any queries you can write them down in the comments section.

Download Source CodeDownload APK

Stay Connected

No spam guarantee.



Abhishek loves coding. His favorite is Android Development. Apart from coding he loves traveling and reading.

Leave a Reply

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