Android Internal Storage Example Tutorial

Published on August 3, 2022

Anupam Chugh

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:

    FileOutputStream fOut = openFileOutput("file name",Context.MODE_PRIVATE);

    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:

    String str = "test data";
  • openFileInput(): This method is used to open a file and read it. It returns an instance of FileInputStream. Its syntax is given below:

    FileInputStream fin = openFileInput(file);

    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:

    int c;
    String temp="";
    while( (c = fin.read()) != -1){
       temp = temp + Character.toString((char)c);

    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

android internal storage example, android file storage

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

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"

        android:text="Android Read and Write Text from/to a File"
        android:textSize="28sp" />


        <requestFocus />

        android:text="Write Text into File"
        android:layout_alignEnd="@+id/editText1" />

        android:text="Read Text From file"
        android:layout_alignStart="@+id/editText1" />


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

package com.journaldev.internalstorage;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class MainActivity extends Activity {

    EditText textmsg;
    static final int READ_BLOCK_SIZE = 100;
    protected void onCreate(Bundle savedInstanceState) {


    // write text to file
    public void WriteBtn(View v) {
        // add-write text into file
        try {
            FileOutputStream fileout=openFileOutput("mytextfile.txt", MODE_PRIVATE);
            OutputStreamWriter outputWriter=new OutputStreamWriter(fileout);

            //display file saved message
            Toast.makeText(getBaseContext(), "File saved successfully!",

        } catch (Exception e) {

    // Read text from file
    public void ReadBtn(View v) {
        //reading text from file
        try {
            FileInputStream fileIn=openFileInput("mytextfile.txt");
            InputStreamReader InputRead= new InputStreamReader(fileIn);

            char[] inputBuffer= new char[READ_BLOCK_SIZE];
            String s="";
            int charRead;

            while ((charRead=InputRead.read(inputBuffer))>0) {
                // char to string conversion
                String readstring=String.copyValueOf(inputBuffer,0,charRead);
                s +=readstring;

        } catch (Exception e) {

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: android file storage, internal storage, Android OpenFileOutput The file “mytextfile.txt” is found in the package name of the project i.e. com.journaldev.internalstorage as shown below: android file storage, internal storage, Android OpenFileOutput Download the final project for android internal storage example from below link.

Download Android Internal Storage Example Project

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Anupam Chugh


While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
DigitalOcean Employee
DigitalOcean Employee badge
May 27, 2021

does it work for API level 30??

- Paramjit Rana

    DigitalOcean Employee
    DigitalOcean Employee badge
    February 26, 2021

    in api level 29 make directory but in android 11 and api level 30 cant make directory so how to save my edited photo and video save in personal directory

    - Hitesh

      DigitalOcean Employee
      DigitalOcean Employee badge
      August 7, 2020

      Hello i have a question… I am capturing photo from camera intent and i can set this as an imageview. But i want to save this captured photo to internal storage. How can i do that? I am new in android area. Sorry if it is a basic question.

      - Muhittin

        DigitalOcean Employee
        DigitalOcean Employee badge
        May 20, 2020

        Hi Anupam, I am facing a problem with file operations in native NDK code. In my native NDK code, i am opening a file and writing some app specific data in it and close it. Later when the app reopens, it tries to read that file and populate app data. When read is done, the content is NULL and thus failing. Any pointers? FIle path is on external storage and file permissions set in manifest.xml. FIle operations being done using fread( ) and fwrite( ) syscalls.

        - Nagesh

          DigitalOcean Employee
          DigitalOcean Employee badge
          March 31, 2020

          hi, can i get the app storage size through java code?

          - vinoth

            DigitalOcean Employee
            DigitalOcean Employee badge
            January 27, 2020

            Is it possible to access the file location by the app itself.? I want to write some info in the file and upload that file to server.

            - Nag Kosanam

              DigitalOcean Employee
              DigitalOcean Employee badge
              October 9, 2019

              great… It is working like a charm Can you please tell be some extra code to save it in a folder, for example InternalStorage/folder/filename.txt Thanks

              - Rupesh Kumar

                DigitalOcean Employee
                DigitalOcean Employee badge
                January 19, 2019

                Thank you man, It was awsome!

                - rômulo

                  DigitalOcean Employee
                  DigitalOcean Employee badge
                  July 26, 2018

                  Thanks a ton for this. I had been looking high and low on how to save and recall strings.

                  - Bil Bo

                    DigitalOcean Employee
                    DigitalOcean Employee badge
                    July 2, 2018

                    I am not able to find this file in my android phone

                    - Anonymus

                      Try DigitalOcean for free

                      Click below to sign up and get $200 of credit to try our products over 60 days!

                      Sign up

                      Join the Tech Talk
                      Success! Thank you! Please check your email for further details.

                      Please complete your information!

                      Featured on Community

                      Get our biweekly newsletter

                      Sign up for Infrastructure as a Newsletter.

                      Hollie's Hub for Good

                      Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

                      Become a contributor

                      Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

                      Welcome to the developer cloud

                      DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

                      Learn more
                      DigitalOcean Cloud Control Panel