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
- <RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/editText"
- android:layout_centerVertical="true"
- android:layout_alignParentLeft="true"
- android:layout_alignParentStart="true" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textAppearance="?android:attr/textAppearanceMedium"
- android:id="@+id/fileContents"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true"
- android:textColor="#ff2c1c"
- android:text="No data Found!" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Load File Contents"
- android:id="@+id/loadFileButton"
- android:layout_below="@+id/fileContents"
- android:layout_centerHorizontal="true" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Save To File"
- android:id="@+id/saveFileButton"
- android:layout_below="@+id/editText"
- android:layout_alignRight="@+id/loadFileButton"
- android:layout_alignEnd="@+id/loadFileButton"
- android:layout_marginTop="42dp" />
- </RelativeLayout>
Bind the ui elements in the onCreate() like the following,
- editText = (EditText) findViewById(R.id.editText);
- final TextView fileContents = (TextView) findViewById(R.id.fileContents);
- Button saveFileButton = (Button) findViewById(R.id.saveFileButton);
- 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,
- FileOutputStream fOut = openFileOutput("mydata.txt", MODE_PRIVATE);
- fOut.write(editText.getText().toString().getBytes());
- fOut.close();
For reading file from data storage use the following code,
- FileInputStream fin = openFileInput("mydata.txt");
- int c;
- String temp = "";
- while ((c = fin.read()) != -1) {
- temp = temp + Character.toString((char) c);
- }
- fileContents.setTextColor(Color.BLUE);
- fileContents.setText(temp);
See the Screen shots also.