Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Internal Storage


We are able to save or read data from the device internal memory. FileInputStream and FileOutputStream classes are used to read and write data into the file.

Here, we are going to read and write data to the internal storage of the device.


Example of reading and writing data to the android internal storage

activity_main.xml

Drag the 2 edittexts, 2 textviews and 2 buttons from the pallete, now the activity_main.xml file will like this:

File: activity_main.xml
  1. <RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”
  2.     xmlns:tools=“http://schemas.android.com/tools”
  3.     android:layout_width=“match_parent”
  4.     android:layout_height=“match_parent”
  5.     tools:context=“.MainActivity” >
  6.     <EditText
  7.         android:id=“@+id/editText1”
  8.         android:layout_width=“wrap_content”
  9.         android:layout_height=“wrap_content”
  10.         android:layout_alignParentRight=“true”
  11.         android:layout_alignParentTop=“true”
  12.         android:layout_marginRight=“20dp”
  13.         android:layout_marginTop=“24dp”
  14.         android:ems=“10” >
  15.         <requestFocus />
  16.     </EditText>
  17.     <EditText
  18.         android:id=“@+id/editText2”
  19.         android:layout_width=“wrap_content”
  20.         android:layout_height=“wrap_content”
  21.         android:layout_alignRight=“@+id/editText1”
  22.         android:layout_below=“@+id/editText1”
  23.         android:layout_marginTop=“24dp”
  24.         android:ems=“10” />
  25.     <TextView
  26.         android:id=“@+id/textView1”
  27.         android:layout_width=“wrap_content”
  28.         android:layout_height=“wrap_content”
  29.         android:layout_alignBaseline=“@+id/editText1”
  30.         android:layout_alignBottom=“@+id/editText1”
  31.         android:layout_alignParentLeft=“true”
  32.         android:text=“File Name:” />
  33.     <TextView
  34.         android:id=“@+id/textView2”
  35.         android:layout_width=“wrap_content”
  36.         android:layout_height=“wrap_content”
  37.         android:layout_alignBaseline=“@+id/editText2”
  38.         android:layout_alignBottom=“@+id/editText2”
  39.         android:layout_alignParentLeft=“true”
  40.         android:text=“Data:” />
  41.     <Button
  42.         android:id=“@+id/button1”
  43.         android:layout_width=“wrap_content”
  44.         android:layout_height=“wrap_content”
  45.         android:layout_alignLeft=“@+id/editText2”
  46.         android:layout_below=“@+id/editText2”
  47.         android:layout_marginLeft=“70dp”
  48.         android:layout_marginTop=“16dp”
  49.         android:text=“save” />
  50.     <Button
  51.         android:id=“@+id/button2”
  52.         android:layout_width=“wrap_content”
  53.         android:layout_height=“wrap_content”
  54.         android:layout_alignBaseline=“@+id/button1”
  55.         android:layout_alignBottom=“@+id/button1”
  56.         android:layout_toRightOf=“@+id/button1”
  57.         android:text=“read” />
  58. </RelativeLayout>

Activity class

Let’s write the code to write and read data from the internal storage.

File: MainActivity.java
  1. package com.example.internalstorage;
  2. import java.io.BufferedReader;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import android.os.Bundle;
  9. import android.app.Activity;
  10. import android.content.Context;
  11. import android.view.Menu;
  12. import android.view.View;
  13. import android.view.View.OnClickListener;
  14. import android.widget.Button;
  15. import android.widget.EditText;
  16. import android.widget.Toast;
  17. public class MainActivity extends Activity {
  18.     EditText editTextFileName,editTextData;
  19.     Button saveButton,readButton;
  20.     @Override
  21.     protected void onCreate(Bundle savedInstanceState) {
  22.         super.onCreate(savedInstanceState);
  23.         setContentView(R.layout.activity_main);
  24.         editTextFileName=(EditText)findViewById(R.id.editText1);
  25.         editTextData=(EditText)findViewById(R.id.editText2);
  26.         saveButton=(Button)findViewById(R.id.button1);
  27.         readButton=(Button)findViewById(R.id.button2);
  28.         //Performing Action on Read Button
  29.         saveButton.setOnClickListener(new OnClickListener(){
  30.             @Override
  31.             public void onClick(View arg0) {
  32.                 String filename=editTextFileName.getText().toString();
  33.                 String data=editTextData.getText().toString();
  34.                 FileOutputStream fos;
  35.                    try {
  36.                     fos = openFileOutput(filename, Context.MODE_PRIVATE);
  37.                     //default mode is PRIVATE, can be APPEND etc.
  38.                     fos.write(data.getBytes());
  39.                     fos.close();
  40.                     Toast.makeText(getApplicationContext(),filename + ” saved”,
  41.                             Toast.LENGTH_LONG).show();
  42.                    } catch (FileNotFoundException e) {e.printStackTrace();}
  43.                    catch (IOException e) {e.printStackTrace();}
  44.             }
  45.         });
  46.         //Performing Action on Read Button
  47.         readButton.setOnClickListener(new OnClickListener(){
  48.             @Override
  49.             public void onClick(View arg0) {
  50.                 String filename=editTextFileName.getText().toString();
  51.                 StringBuffer stringBuffer = new StringBuffer();
  52.                 try {
  53.                     //Attaching BufferedReader to the FileInputStream by the help of InputStreamReader
  54.                     BufferedReader inputReader = new BufferedReader(new InputStreamReader(
  55.                             openFileInput(filename)));
  56.                     String inputString;
  57.                     //Reading data line by line and storing it into the stringbuffer              
  58.                     while ((inputString = inputReader.readLine()) != null) {
  59.                         stringBuffer.append(inputString + “\n”);
  60.                     }
  61.                 } catch (IOException e) {
  62.                     e.printStackTrace();
  63.                 }
  64.                 //Displaying data on the toast
  65.                 Toast.makeText(getApplicationContext(),stringBuffer.toString(),
  66.                         Toast.LENGTH_LONG).show();
  67.             }
  68.         });
  69.     }
  70.     @Override
  71.     public boolean onCreateOptionsMenu(Menu menu) {
  72.         // Inflate the menu; this adds items to the action bar if it is present.
  73.         getMenuInflater().inflate(R.menu.activity_main, menu);
  74.         return true;
  75.     }
  76. }

Comments

comments