---
title: "Android Persistence with Internal Storage"  
description: "Hi everyone, In this article I a describing Android Persistence with Internal Storage"  
author: "Manoj Pandey"  
published: 2015-02-19  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/1591/android-persistence-with-internal-storage  
category: "android"  
tags: ["android"]  
reading_time: 7 minutes  

---

# Android Persistence with Internal Storage

##

In my last article, we learn about XmlPullParser in android. Now we see how to use Android [Persistence](https://www.mindstick.com/interview/848/what-is-meant-by-persistence) with [Internal Storage](https://answers.mindstick.com/qa/51515/how-much-internal-storage-does-each-iphone-model-have-can-internal-storage-be-expanded-is-it-possible-to-add-external-storage-does-the-iphone-have-a-microsd-slot)

Android provides several options for you to save persistent application data. The solution you choose depends on your specific needs, such as whether the data should be private to [your application](https://answers.mindstick.com/qa/95487/how-do-you-troubleshoot-when-your-application-link-is-not-responding) or accessible to other [applications](https://www.mindstick.com/articles/12847/how-to-choose-the-right-ethernet-cable-for-industrial-applications) (and the user) and how much space your data requires.

Your data [storage options](https://www.mindstick.com/forum/158882/what-are-the-different-types-of-storage-options-available-in-android) are the following:

1. Internal Storage

2. Shared Preferences

3. [External Storage](https://answers.mindstick.com/qa/51623/what-external-storage-options-are-available-for-the-touch-bar-macbook-pro-are-adapters-required)

4. SQLite Databases

5. [Network Connection](https://answers.mindstick.com/qa/94984/how-do-you-change-the-network-connection-priority-in-windows-10)

\
Here, I am using Android Persistence Internal Storage.

1. Create an Android project.

2. Add controls in activity_main.xml

```
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:orientation="vertical"
    android:paddingTop="30sp"
    tools:context=".MainActivity" >

    <EditText
          android:id="@+id/myEditText"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_gravity="center"
          android:layout_marginLeft="10sp"
          android:layout_marginRight="10sp"
          android:layout_marginTop="10sp"
          android:hint="Enter text"   />

    <Button
          android:id="@+id/mybtn"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="center"
          android:layout_marginTop="20sp"
          android:text="  Save    " />

    <Button
          android:id="@+id/btngetData"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="center"
          android:layout_marginTop="20sp"
          android:text="Retrieve Data"   />

  </LinearLayout> 
```

3. Now add following code in

##### MainActivity.

```
 package  com.example.androiddemopersistenceexample;
 
  import java.io.BufferedReader;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.OutputStreamWriter;
 
  import android.os.Bundle;
  import android.app.Activity;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.Toast;
 
  public class MainActivity extends Activity {
     private final static String STORETEXT = "Sample.txt";
     private EditText edittext;
     Button btnSave, btngetData;
 
     @Override
     protected void onCreate(Bundle  savedInstanceState) {
           super.onCreate(savedInstanceState);
 
           setContentView(R.layout.activity_main);
 
           edittext = (EditText) findViewById(R.id.myEditText);
           btnSave = (Button) findViewById(R.id.mybtn);
           btngetData = (Button) findViewById(R.id.btngetData);
 
           // This method retrieve data from file and fill value in edit  text
           readDatafromFile();
 
           // save button click listener
           btnSave.setOnClickListener(new OnClickListener() {
 
                @Override
                public void onClick(View v) {
                     // TODO Auto-generated method stub
                     try {
 
                           // Create file name if file not exist
                           OutputStreamWriter outwriter = new OutputStreamWriter(
                                     openFileOutput(STORETEXT, 0));
 
                           // Writes the character oneChar to this writer.
                          outwriter.write(edittext.getText().toString());
 
                           // Closes this writer.
                           outwriter.close();
 
                           // show toast message after  save record
                          Toast.makeText(getApplicationContext(),   "Saved  data",
                                     Toast.LENGTH_SHORT).show();
 
                           // after add data clear edittext
                           edittext.setText("");
 
                     } catch (Exception e) {
 
                           // if any problem to save data or exception
                          Toast.makeText(getApplicationContext(),
                                     "Data could not save", Toast.LENGTH_SHORT).show();
                     }
                }
           });
           // Retrieve button click listener
           btngetData.setOnClickListener(new OnClickListener() {
 
                @Override
                public void onClick(View v) {
 
                     readDatafromFile();
                }
           });
     }
 
     public void readDatafromFile() {
           try {
                // A readable source of bytes.
                InputStream inputStream = openFileInput(STORETEXT);
 
                if (inputStream != null) {
 
                     // Data read from the source input stream is converted into
                     // characters by either a default or a provided character
                     // converter
                     InputStreamReader inputStreamReader = new InputStreamReader(
                                inputStream);
                     // BufferedReader wraps an existing Reader and buffers the
                     // input.
                     BufferedReader brReader = new  BufferedReader(inputStreamReader);
 
                     String str;
 
                     // StringBuilder is a modifiable sequence of characters for  use
                     // in creating strings.
                     StringBuilder buf = new StringBuilder();
 
                     while ((str = brReader.readLine()) != null) {
 
                           // append value in stringbuilder
                           buf.append(str);
 
                     }
 
                     // Closes this stream.
                     inputStream.close();
                     edittext.setText(buf.toString());
                }
           } catch (Exception ex) {
           }
 
     }} 
```

And run your application

\
![Android Persistence with Internal Storage](https://www.mindstick.com/mindstickarticle/3cb77e47-9370-4058-9a99-8801ee06f06d/images/b91a1a89-1020-47a2-8157-28a2578f5fec.png) ![Android Persistence with Internal Storage](https://www.mindstick.com/mindstickarticle/3cb77e47-9370-4058-9a99-8801ee06f06d/images/30cec635-b991-41b0-b7f4-35046d0feaa1.png)

Close [your activity](https://www.mindstick.com/interview/12746/the-last-callback-in-the-lifecycle-of-an-activity-is-ondestroy-the-system-calls-this-method-on-your-activity-as-the-final-signal-that-your-activity-instance-is-being-completely-removed-from-the-sys) and reopen this application

![Android Persistence with Internal Storage](https://www.mindstick.com/mindstickarticle/3cb77e47-9370-4058-9a99-8801ee06f06d/images/c89d0950-7438-446f-bafb-cfd3dbcf9863.png)

---

Original Source: https://www.mindstick.com/articles/1591/android-persistence-with-internal-storage

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
