Internal File Storage In Android

Introduction

 
Android system provides lots of ways to store data within the device like internal or external storage, SQL storage and shared preferences.
Files stored in internal memory will be there whenever the application is there in the device, but by storing in the external storage this will be there if the application is uninstalled by default. And the internal storage is not accessible from the outside world of the application.
 
In order to write to the private storage call openFileOutput() with file name and mode (MODE_PRIVATE). This will create a file in the data storage of the application. Here I am going to show you how to write to data storage of the Android device and how we can read from the data storage of an application in the Android device. In this application, I have an edit text Load file button, Save File Button and a Text View.
 
In which when I click on the save button the contents in the edittext will be saved to a file in the data storage. When I click on the load button the contents of the file will be read from the data storage and will be set to the textview in the UI. First of all, I need to create the main layout.
 
activity_main.xml 
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2. xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"  
  3. android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"  
  4. android:paddingRight="@dimen/activity_horizontal_margin"  
  5. android:paddingTop="@dimen/activity_vertical_margin"  
  6. android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">  
  7.     <EditText  
  8. android:layout_width="match_parent"  
  9. android:layout_height="wrap_content"  
  10. android:id="@+id/editText"  
  11. android:layout_centerVertical="true"  
  12. android:layout_alignParentLeft="true"  
  13. android:layout_alignParentStart="true" />  
  14.     <TextView  
  15. android:layout_width="wrap_content"  
  16. android:layout_height="wrap_content"  
  17. android:textAppearance="?android:attr/textAppearanceMedium"  
  18. android:id="@+id/fileContents"  
  19. android:layout_alignParentTop="true"  
  20. android:layout_centerHorizontal="true"  
  21. android:textColor="#ff2c1c"  
  22. android:text="No data Found!" />  
  23.     <Button  
  24. android:layout_width="wrap_content"  
  25. android:layout_height="wrap_content"  
  26. android:text="Load File Contents"  
  27. android:id="@+id/loadFileButton"  
  28. android:layout_below="@+id/fileContents"  
  29. android:layout_centerHorizontal="true" />  
  30.     <Button  
  31. android:layout_width="wrap_content"  
  32. android:layout_height="wrap_content"  
  33. android:text="Save To File"  
  34. android:id="@+id/saveFileButton"  
  35. android:layout_below="@+id/editText"  
  36. android:layout_alignRight="@+id/loadFileButton"  
  37. android:layout_alignEnd="@+id/loadFileButton"  
  38. android:layout_marginTop="42dp" />  
  39. </RelativeLayout>   
Bind the ui elements in the onCreate() like the following,
  1. editText = (EditText) findViewById(R.id.editText);  
  2. final TextView fileContents = (TextView) findViewById(R.id.fileContents);  
  3. Button saveFileButton = (Button) findViewById(R.id.saveFileButton);  
  4. Button loadButton = (Button) findViewById(R.id.loadFileButton);  
Now we need to create the listeners for both of the buttons for loading file from data storage and saving 
 
Please see the code for saving file to data storage,
  1. FileOutputStream fOut = openFileOutput("mydata.txt", MODE_PRIVATE);  
  2. fOut.write(editText.getText().toString().getBytes());  
  3. fOut.close();  
For reading file from data storage use the following code,
  1. FileInputStream fin = openFileInput("mydata.txt");  
  2. int c;  
  3. String temp = "";  
  4. while ((c = fin.read()) != -1) {  
  5.     temp = temp + Character.toString((char) c);  
  6. }  
  7. fileContents.setTextColor(Color.BLUE);  
  8. fileContents.setText(temp);  
See the Screen shots also.
 
 
 
 
 


Similar Articles