Android Read Write File Operations

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 – >

 

COMMENTS (2)
    Reply

    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

Leave a Comment

Comment (required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Name (required)
Email (required)