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

If you liked this article, then share it with your friends.

Related Tutorials

Anatomy of an android app
Anatomy of an Android App
  • July 18, 2025
  • Com 0

Anatomy of an Android App Anatomy of an Android app refers to its core structure and essential components that work…

Roomdatabase in android image
Mastering room database using kotlin: Complete Guide with CRUD Operations
  • April 4, 2025
  • Com 0

Local data storage is essential for building modern Android apps that work offline and store data persistently. Whether you’re creating…

Android SDK Image
Android SDK and It’s Components
  • March 27, 2025
  • Com 0

Android SDK stands for Android Software Development Kit which is developed by Google for Android Platform with the help of…

Android Architecture Image
Android Architecture
  • March 20, 2025
  • Com 0

Android architecture is the foundation and structure that makes an Android device work. Android architecture consists of layers that work together to run apps…

Content Provider in Android Feature Image
Content Provider in Android
  • February 7, 2025
  • Com 0

Content Provider in Android is a component of the Android application that is useful for sharing data with other apps.…

SQLite Database in Android
SQLite Database in Android
  • January 28, 2025
  • Com 0

Android app or any application is made of two things Data And Functionality. So In Any application we need a…

Get Your Free Java
Data Structures Cheat Sheet! 🎁

Download the ultimate cheat sheet covering the Top 10 Data Structures every Java developer must know — with real-life examples! Just enter your email and get the free pdf.

We respect your privacy. No spam. Read our privacy policy for more information.