Home » Kotlin Android Read and Write Internal Storage

Kotlin Android Read and Write Internal Storage

by Online Tutorials Library

Kotlin Android Read and Write Internal Storage

Android Internal Storage is the device memory in which we store the files. The file stored in the internal storage is private in default, and only the same application accesses it. They cannot be accessed from outside the application.

To read and write the data from (into) the file, Android provides openFileInput() and openFileOutput() methods respectively.

When the users uninstall its application from the device, its internal storage file will also be removed.

Write to File in Internal Storage

To write the file in internal storage of device, java.io package offers openFileOutput() method which returns the instance of FileOutputStream class. To write the data into the file call the FileOutputStream .write() method.

Read File content from Internal Storage

To read the file from the internal storage of device, java.io package offers openFileInput() method which returns the instance of FileInputStream class. To read the data from file call the BufferedReader().readLine()

Kotlin Android Read and Write Internal Storage Example

In this example, we will write the data to file inside the internal storage and read the same file content from internal storage.

activity_main.xml

Add the following code in the activity_main.xml file. In this file, add two EditText for input file name, file content and two Button for saving and view file content.

MainActivity.kt

Add the following code in the MainActivity.kt class. In this class, we are saving the file name and data inside internal storage by clicking the save button and retrieving the file content by clicking view button.

Output:

Kotlin Android Read and Write Internal Storage Kotlin Android Read and Write Internal Storage
Kotlin Android Read and Write Internal Storage Kotlin Android Read and Write Internal Storage
Kotlin Android Read and Write Internal Storage

Next Topic#

You may also like