Every android application needs a way to store data, Android offers a few ways to store data, which Includes:

  1. Internal Storage
  2. External Storage
  3. SharedPreferences
  4. SQLite Database
  5. Cloud Storage (via Network)

In this tutorial we will use Android internal storage to store and read data into files.

Android Internal Storage

Android Internal storage is Device internal memory and that can be used to store data or files. The File or data stored on internal storage is private by default, and can only be accessed by the same application and can not be accessed by other apps.

  • To Perform Write Operation on Internal Storage openFileOutput() method is used
  • To Perform Read Operation on Internal Storage openFileInput() method is used

Write file in Internal Storage

To Perform write operation on internal Storage we will be using openFileOutput() method.
This method is used to create and save file.  

Syntax:
				
					openFileOutput(String filename, int mode)
				
			

Here, the first parameter is the filename and the second parameter is MODE. In Mode parameter you can pass Context.MODE_PRIVATE, this will create a new file in private mode and if file exists then it will override. You can also pass Context.MODE_APPEND, this will create a new file in append mode and If file already exists then the data is appended at the end of the file.

				
					FileOutputStream fileOutputStream = openFileOutput("filename", Context.MODE_PRIVATE);
				
			

openFileOutput() method returns the instance of FileOutputStream. FileOutputStream is an outputstream for writing data streams of raw bytes to file and we will use the write method of FileOutputStream to write data of bytes on file.     

				
					private void writeData(String fileName, String fileData) {
    try {
        
        FileOutputStream fileOutputStream = openFileOutput(fileName, MODE_PRIVATE);
        fileOutputStream.write(fileData.getBytes());
        fileOutputStream.flush();
        fileOutputStream.close();

    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
				
			

Read file from Internal Storage

To Perform read operation on internal Storage we will be using openFileInput() method.
This method is used to read files from Internal Storage and this method is part of java.io package in java.  

Syntax:
				
					openFileInput(String filename)
				
			

Here, the only parameter passed is filename

				
					FileInputStream fileInputStream = openFileInput("filename");
				
			

This method returns the instance of FileInputStream. FileInputStream class in Java is useful for reading data from a file in the form of a sequence of bytes.      

				
					private String readData(String fileName) {

    try {
        FileInputStream fileInputStream = openFileInput(fileName);
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        StringBuilder stringBuilder = new StringBuilder();
        String text = null;
        
        while ((text = bufferedReader.readLine()) != null) {
            stringBuilder.append(text);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
				
			

Here, In readData() method InputStreamReader is used to convert the sequence of bytes into the character streams and BufferedReader class’s readLine() method is used to read data from file.        

Step by Step Code Implementation

Here, we have created a app which will write and read data from the file (Internal Storage). 

File : activity_main.xml 

The activity_main.xml layout file contains two EditText, one for the filename and one to write data and two buttons to read data from the file and to write data on the file.  

The MainActivity contains the implementation of the reading and writing to files from the internal – storage.

MainActivity: Kotlin/Java File  

Output:

Download the final project for android internal storage example from below link : 

Android Internal Storage Example Code