Android Read Write File Operations - Androhub

Text File Banner

Android Read Write File Operations

Reading and Writing files in Android will be helpful if you want to read data from SD Card, store your application data as a text file. In this tutorial we are going to learn same. Below are some points that we are going to learn in today’s tutorial:

1. Write File – Write user input data into TextFile.

2. Read File – Read data from saved user data file.

3. Delete File – Delete saved file from SD Card.

4. TimeStamp – Fetching current time stamp and saved in shared preferences when file is saved.

5. File Path –  Display file path to locate where the Text File is saved.

Prerequisites required for this tutorial –

We are going to use two classes  for writing and reading file to SD Card –

1. FileWriter –  A specialised Writer that writes to a file in the file system. All write requests made by calling methods in this class are directly forwarded to the equivalent function of the underlying operating system. Since this may induce some performance penalty, in particular if many small write requests are made, a FileWriter is often wrapped by a BufferedWriter.

2. FileReader – A specialised Reader that reads from a file in the file system. All read requests made by calling methods in this class are directly forwarded to the equivalent function of the underlying operating system. Since this may induce some performance penalty, in particular if many small read requests are made, a FileReader is often wrapped by a BufferedReader.

Example

In this tutorial, we are going to learn how to write, read and delete File operations in Android.

VIDEO DEMO

1. Create a new project in Android Studio by navigating to File ⇒ New Android ⇒ Application Project and fill required details. By default my activity is MainActivity.java.

2. Open res=>values=strings.xml and add the following strings to it.

3. Create a xml layout naming activity_main.xml and add the following code. In this  layout there is one EditText to take user input and some buttons for writing, reading and delete text file and a hidden layout for showing saved text file data.

4. Create a drawable under drawable  directory naming edittext_border.xml for giving border to EditText.

5. Create a CheckForSDCard.java class for checking if SDCard is mounted or not.

6. Now below are methods that i am using for writing , reading and deleting text files.

  • Write File Method

  •  Read File Method – Here ‘fileDirectory’ is main directory and ‘savedFile’ is our TextFile. This method will return us string data after reading text file.

  • Delete File Method –

  • TimeStamp Code –

7. Finally come to your MainActivity.java and add the following code.

8. Finally, all done you can use above code to save your data in File.

Thanks. :)

Download Source Code – >

 

6 Comments

Bhushan
Tuesday, February 23rd, 2016

Hi. Great tutorial. However, it does’t work on Android 6.0 due to changed in write permissions. Here’s a link to get an idea of how to implement write permissions:

http://stackoverflow.com/questions/33139754/android-6-0-marshmallow-cannot-write-to-sd-card

Can u please rewrite the code by using this? or at least guild me how to do it to make this app run on MM. Thanks.

Droid
Tuesday, February 23rd, 2016

Hi Bhushan,

Do the below following steps for MarshMallow Device :

1. private static final int REQUEST_WRITE_STORAGE = 112;
declare this as global variable for Request Id.

2. While reading and writing on SD Card use this code before doing that task

boolean hasPermission = (ContextCompat.checkSelfPermission(activity,
Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
if (!hasPermission) {
ActivityCompat.requestPermissions(parentActivity,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_WRITE_STORAGE);
}
else{
//Call read or write method}

3. Now to access request permission that user is accepted to grant permission then use this method for acceptance and denial

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode)
{
case REQUEST_WRITE_STORAGE: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
//Call read or write method
} else
{
Toast.makeText(parentActivity, “The app was not allowed to write to your storage. Hence, it cannot function properly. Please consider granting it this permission”, Toast.LENGTH_LONG).show();
}
}
}

}

Hope it helps.

Thanks

hassan
Friday, April 7th, 2017

can you sent the coding for this tutorial.. i’m using android 6.0.. realy appreciate that.. tq

Dr. Droid
Saturday, April 8th, 2017

Hi Hassan,

Please click on Download Source Code again. I replaced new code having Marshmallow permission. Hope it will help you.

Thanks

hashir
Wednesday, April 22nd, 2020

Having issues in downloading the code. Its a 404

Dr. Droid
Thursday, May 7th, 2020

Hi Hashir,

You can download the code from here: https://github.com/sonusurender/Read_Write_File_Operations_Demo

Thanks

Post comment

Your email address will not be published. Required fields are marked *