![]() |
VOOZH | about |
The aim of this article is to show users how to use internal storage. In this article will be creating an application that can write data to a file and store it in internal storage and read data from the file and display it on the main activity using TextView. Saving and loading data on the internal storage is private for an application that can not be accessed by other applications. When the app is uninstalled the data stored in the internal by that app is removed. To read and write in the Android internal storage we have two methods
1. OpenFileOutput(): used for creating and saving a file. This method returns a FileOutputStream instance.
Syntax:
OpenFileOutput(String filename,int mode)Parameters:
mode:
Returns: FileOutputStream object
2. OpenFileInput(): Used to read data from a file, this returns a FileInputStream instance.
Syntax:
OpenFileInput( String filename)Returns: FileInputStream object
A sample video is given below to get an idea about what we are going to do in this article.
Note: that we are going to implement this project using Java.
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
The activity_main.xml file has the following widgets
Below is the code for the activity_main.xml file.
Inside the MainActivity.java file we are going to do the following things:
Initialize variables:
The file will be creating is myfile.txt. this can be found in Device File Explorer > data > data > application_package > files
The following method is used to read data from the internal data.
The method used for creating a file and writing data to internal storage.
Below is the complete code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.