Internal Storage Android
How to write or read file in internal storage.
Hi beginners we create blogs that will help you to find write minimum lines of code with proper understanding of a topic.
Internal Storage store file & delete when app is uninstalled so let’s find out how to read or write a file in internal storage.
So in Android when we have to write a file under internal storage it will create files folder under data -> package_name ->files have a look to below figure:
Now we have to create a JAVA class & xml to write the code for read & write file in Internal Storage.
activity_store_file_internal.xml under layout.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".StoreFileInternalActivity">
<include layout="@layout/custom_layout"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Internal Storage"
android:layout_marginTop="40dp"
android:textSize="30sp"
android:textStyle="bold"
android:id="@+id/title"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:layout_width="match_parent"
android:layout_height="200dp"
android:hint="Write Something"
android:textSize="20sp"
android:gravity="start"
android:padding="5dp"
android:layout_margin="20dp"
android:id="@+id/ed_add_something"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/title" />
<Button
android:id="@+id/btn_write"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:padding="10dp"
android:text="Write"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/ed_add_something" />
<Button
android:id="@+id/btn_read"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:padding="10dp"
android:text="Read"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toRightOf="@id/btn_write"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/ed_add_something" />
</androidx.constraintlayout.widget.ConstraintLayout>
Now in StoreFileInternalActivity.java we initializing view from xml & then create a file UKTechiansInfo.txt, “.txt” is an extension of a file. Using FileOutputStream to write text in a file. Setting up file mode as MODE_PRIVATE so that only this application can access the file. And using FileInputStream to read using StringBuffer(). When ever we use FileInputStream or FileOutputStream it should be called under try-catch block to handle exceptions (i.e if file is not created or not found in the desire folder it will throw an exception under catch block).
package com.demo.storageinandroid;
import androidx.appcompat.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.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class StoreFileInternalActivity extends AppCompatActivity implements View.OnClickListener {
EditText ed_write_file;
Button btn_write_data, btn_read_data;
String fileName = "UKTechiansInfo.txt";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_store_file_internal);
init();
}
private void init() {
ed_write_file = (EditText) findViewById(R.id.ed_add_something);
btn_read_data = (Button) findViewById(R.id.btn_read);
btn_write_data = (Button) findViewById(R.id.btn_write);
btn_read_data.setOnClickListener(this);
btn_write_data.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (view.getId()==R.id.btn_write){
writeToFile();
}
if (view.getId()==R.id.btn_read){
readToFile();
}
}
private void writeToFile() {
try {
FileOutputStream fos = openFileOutput(fileName, MODE_PRIVATE);
fos.write(ed_write_file.getText().toString().getBytes());
fos.close();
ed_write_file.setText("");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void readToFile() {
StringBuffer stringBuffer = new StringBuffer();
try {
FileInputStream fis = openFileInput(fileName);
int i = 0;
while ((i = fis.read())!=-1){
stringBuffer.append((char)i);
}
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
ed_write_file.setText(stringBuffer);
}
}
We have to use fos.close to close a stream after writing the text to the file. And fis.close to close a stream after reading text from a file.
Follow us on Facebook: https://www.facebook.com/UKTechiansTutorials