Industrial Training




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.


val file:String = fileName.text.toString()  
val data:String = fileData.text.toString()  
val fileOutputStream:FileOutputStream  
try {  
    fileOutputStream = openFileOutput(file, Context.MODE_PRIVATE)  
    fileOutputStream.write(data.toByteArray())  
}catch (e: Exception){  
    e.printStackTrace()  
}  
val file:String = fileName.text.toString()  
val data:String = fileData.text.toString()  
val fileOutputStream:FileOutputStream  
try {  
    fileOutputStream = openFileOutput(file, Context.MODE_PRIVATE)  
    fileOutputStream.write(data.toByteArray())  
}catch (e: Exception){  
    e.printStackTrace()  
}  

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()


var fileInputStream: FileInputStream? = null  
fileInputStream = openFileInput(filename)  
var inputStreamReader: InputStreamReader = InputStreamReader(fileInputStream)  
val bufferedReader: BufferedReader = BufferedReader(inputStreamReader)  
val stringBuilder: StringBuilder = StringBuilder()  
var text: String? = null  
while ({ text = bufferedReader.readLine(); text }() != null) {  
    stringBuilder.append(text)  
}  
//Displaying data on EditText  
fileData.setText(stringBuilder.toString()).toString()  
var fileInputStream: FileInputStream? = null  
fileInputStream = openFileInput(filename)  
var inputStreamReader: InputStreamReader = InputStreamReader(fileInputStream)  
val bufferedReader: BufferedReader = BufferedReader(inputStreamReader)  
val stringBuilder: StringBuilder = StringBuilder()  
var text: String? = null  
while ({ text = bufferedReader.readLine(); text }() != null) {  
    stringBuilder.append(text)  
}  
//Displaying data on EditText  
fileData.setText(stringBuilder.toString()).toString()  

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.


< ?xml version="1.0" encoding="utf-8"?>  
< RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:app="http://schemas.android.com/apk/res-auto"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    tools:context="example.javatpoint.com.kotlininternalstoragereadwrite.MainActivity">  
  
    < TextView  
        android:id="@+id/textView"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentLeft="true"  
        android:layout_alignParentStart="true"  
        android:layout_alignParentTop="true"  
        android:layout_marginLeft="31dp"  
        android:layout_marginStart="31dp"  
        android:layout_marginTop="117dp"  
        android:text="File Name"  
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"/>  
  
    < EditText  
        android:id="@+id/editFile"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignBaseline="@+id/textView"  
        android:layout_alignBottom="@+id/textView"  
        android:layout_alignLeft="@+id/btnSave"  
        android:layout_alignStart="@+id/btnSave"  
        android:layout_marginLeft="31dp"  
        android:layout_marginStart="31dp"  
        android:ems="10"  
        android:paddingLeft="10dp"  
        android:inputType="textPersonName"  
        android:hint="file name" />  
  
    < TextView  
        android:id="@+id/textView2"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignEnd="@+id/textView"  
        android:layout_alignRight="@+id/textView"  
        android:layout_below="@+id/textView"  
        android:layout_marginTop="67dp"  
        android:text="File Data"  
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"/>  
  
    < EditText  
        android:id="@+id/editData"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignBaseline="@+id/textView2"  
        android:layout_alignBottom="@+id/textView2"  
        android:layout_alignLeft="@+id/editFile"  
        android:layout_alignStart="@+id/editFile"  
        android:ems="10"  
        android:lines="5"  
        android:hint="data" />  
  
    < Button  
        android:id="@+id/btnSave"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignLeft="@+id/textView2"  
        android:layout_alignParentBottom="true"  
        android:layout_alignStart="@+id/textView2"  
        android:layout_marginBottom="53dp"  
        android:layout_marginLeft="49dp"  
        android:layout_marginStart="49dp"  
        android:text="Save" />  
  
    < Button  
        android:id="@+id/btnView"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignEnd="@+id/editData"  
        android:layout_alignRight="@+id/editData"  
        android:layout_alignTop="@+id/btnSave"  
        android:layout_marginEnd="33dp"  
        android:layout_marginRight="33dp"  
        android:text="View" />  
< /RelativeLayout>  
< ?xml version="1.0" encoding="utf-8"?>  
< RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:app="http://schemas.android.com/apk/res-auto"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    tools:context="example.javatpoint.com.kotlininternalstoragereadwrite.MainActivity">  
  
    < TextView  
        android:id="@+id/textView"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentLeft="true"  
        android:layout_alignParentStart="true"  
        android:layout_alignParentTop="true"  
        android:layout_marginLeft="31dp"  
        android:layout_marginStart="31dp"  
        android:layout_marginTop="117dp"  
        android:text="File Name"  
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"/>  
  
    < EditText  
        android:id="@+id/editFile"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignBaseline="@+id/textView"  
        android:layout_alignBottom="@+id/textView"  
        android:layout_alignLeft="@+id/btnSave"  
        android:layout_alignStart="@+id/btnSave"  
        android:layout_marginLeft="31dp"  
        android:layout_marginStart="31dp"  
        android:ems="10"  
        android:paddingLeft="10dp"  
        android:inputType="textPersonName"  
        android:hint="file name" />  
  
    < TextView  
        android:id="@+id/textView2"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignEnd="@+id/textView"  
        android:layout_alignRight="@+id/textView"  
        android:layout_below="@+id/textView"  
        android:layout_marginTop="67dp"  
        android:text="File Data"  
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"/>  
  
    < EditText  
        android:id="@+id/editData"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignBaseline="@+id/textView2"  
        android:layout_alignBottom="@+id/textView2"  
        android:layout_alignLeft="@+id/editFile"  
        android:layout_alignStart="@+id/editFile"  
        android:ems="10"  
        android:lines="5"  
        android:hint="data" />  
  
    < Button  
        android:id="@+id/btnSave"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignLeft="@+id/textView2"  
        android:layout_alignParentBottom="true"  
        android:layout_alignStart="@+id/textView2"  
        android:layout_marginBottom="53dp"  
        android:layout_marginLeft="49dp"  
        android:layout_marginStart="49dp"  
        android:text="Save" />  
  
    < Button  
        android:id="@+id/btnView"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignEnd="@+id/editData"  
        android:layout_alignRight="@+id/editData"  
        android:layout_alignTop="@+id/btnSave"  
        android:layout_marginEnd="33dp"  
        android:layout_marginRight="33dp"  
        android:text="View" />  
< /RelativeLayout>  

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.


package example.javatpoint.com.kotlininternalstoragereadwrite  
  
import android.content.Context  
import android.support.v7.app.AppCompatActivity  
import android.os.Bundle  
import android.view.View  
import android.widget.Button  
import android.widget.EditText  
import android.widget.Toast  
import java.io.*  
  
  
class MainActivity : AppCompatActivity() {  
  
    override fun onCreate(savedInstanceState: Bundle?) {  
        super.onCreate(savedInstanceState)  
        setContentView(R.layout.activity_main)  
  
        val fileName = findViewById< EditText>(R.id.editFile)  
        val fileData = findViewById< EditText>(R.id.editData)  
  
        val btnSave = findViewById< Button>(R.id.btnSave)  
        val btnView = findViewById< Button>(R.id.btnView)  
  
        btnSave.setOnClickListener(View.OnClickListener {  
            val file:String = fileName.text.toString()  
            val data:String = fileData.text.toString()  
            val fileOutputStream:FileOutputStream  
            try {  
                fileOutputStream = openFileOutput(file, Context.MODE_PRIVATE)  
                fileOutputStream.write(data.toByteArray())  
            } catch (e: FileNotFoundException){  
                e.printStackTrace()  
            }catch (e: NumberFormatException){  
                e.printStackTrace()  
            }catch (e: IOException){  
                e.printStackTrace()  
            }catch (e: Exception){  
                e.printStackTrace()  
            }  
            Toast.makeText(applicationContext,"data save",Toast.LENGTH_LONG).show()  
            fileName.text.clear()  
            fileData.text.clear()  
        })  
  
        btnView.setOnClickListener(View.OnClickListener {  
                val filename = fileName.text.toString()  
                if(filename.toString()!=null && filename.toString().trim()!=""){  
                   var fileInputStream: FileInputStream? = null  
                   fileInputStream = openFileInput(filename)  
                   var inputStreamReader: InputStreamReader = InputStreamReader(fileInputStream)  
                   val bufferedReader: BufferedReader = BufferedReader(inputStreamReader)  
                   val stringBuilder: StringBuilder = StringBuilder()  
                   var text: String? = null  
                   while ({ text = bufferedReader.readLine(); text }() != null) {  
                       stringBuilder.append(text)  
                   }  
                   //Displaying data on EditText  
                   fileData.setText(stringBuilder.toString()).toString()  
               }else{  
                   Toast.makeText(applicationContext,"file name cannot be blank",Toast.LENGTH_LONG).show()  
               }  
        })  
  
    }  
}  
package example.javatpoint.com.kotlininternalstoragereadwrite  
  
import android.content.Context  
import android.support.v7.app.AppCompatActivity  
import android.os.Bundle  
import android.view.View  
import android.widget.Button  
import android.widget.EditText  
import android.widget.Toast  
import java.io.*  
  
  
class MainActivity : AppCompatActivity() {  
  
    override fun onCreate(savedInstanceState: Bundle?) {  
        super.onCreate(savedInstanceState)  
        setContentView(R.layout.activity_main)  
  
        val fileName = findViewById< EditText>(R.id.editFile)  
        val fileData = findViewById< EditText>(R.id.editData)  
  
        val btnSave = findViewById < Button>(R.id.btnSave)  
        val btnView = findViewById< Button>(R.id.btnView)  
  
        btnSave.setOnClickListener(View.OnClickListener {  
            val file:String = fileName.text.toString()  
            val data:String = fileData.text.toString()  
            val fileOutputStream:FileOutputStream  
            try {  
                fileOutputStream = openFileOutput(file, Context.MODE_PRIVATE)  
                fileOutputStream.write(data.toByteArray())  
            } catch (e: FileNotFoundException){  
                e.printStackTrace()  
            }catch (e: NumberFormatException){  
                e.printStackTrace()  
            }catch (e: IOException){  
                e.printStackTrace()  
            }catch (e: Exception){  
                e.printStackTrace()  
            }  
            Toast.makeText(applicationContext,"data save",Toast.LENGTH_LONG).show()  
            fileName.text.clear()  
            fileData.text.clear()  
        })  
  
        btnView.setOnClickListener(View.OnClickListener {  
                val filename = fileName.text.toString()  
                if(filename.toString()!=null && filename.toString().trim()!=""){  
                   var fileInputStream: FileInputStream? = null  
                   fileInputStream = openFileInput(filename)  
                   var inputStreamReader: InputStreamReader = InputStreamReader(fileInputStream)  
                   val bufferedReader: BufferedReader = BufferedReader(inputStreamReader)  
                   val stringBuilder: StringBuilder = StringBuilder()  
                   var text: String? = null  
                   while ({ text = bufferedReader.readLine(); text }() != null) {  
                       stringBuilder.append(text)  
                   }  
                   //Displaying data on EditText  
                   fileData.setText(stringBuilder.toString()).toString()  
               }else{  
                   Toast.makeText(applicationContext,"file name cannot be blank",Toast.LENGTH_LONG).show()  
               }  
        })  
  
    }  
} 





Hi I am Pluto.