Write Text File to Internal Storage – Android Studio Tutorial



In this video we will save a string to the internal storage of our device. We will use a FileOutputStream to save the data in bytes. Then we use a FileInputStream and a BufferedReader to read them back into our app and rebuild our string using a StringBuilder. We will also catch any IOExceptions.

Source code:

____________________
❗ Subscribe to the channel:

🎓 Find more tutorials and courses on my website:

💻 Hire me for your next project:

📨 Subscribe to the Coding in Flow newsletter:

💚 Donate to Coding in Flow with a message:

❓ Ask your programming questions in our chat:

📣 Follow Coding in Flow on other sites:
Facebook:
Instagram:
Twitter:
Github:

⌨️ My gear and tools:
Microphone:
Mouse:
Keyboard:
Desk:
Monitor:
Webcam:
Screen recorder:

(above links are affiliate links, which means that I earn a small commission if you buy through them with NO additional costs to you.)

35 Replies to “Write Text File to Internal Storage – Android Studio Tutorial”

  1. GURSES

    How can i use to append because data is updated every time. I don't want this I want it to continue from the last String. Or what can I use instead of Append for this, thanks for everything.

  2. António Vidal

    Hello! I have a question to you. Not directly related with the code but with the recording. What tool do you use to make zoom of the code when you are recording?
    Thank you.

  3. Niral Naik

    Save app data to Google drive as a backup

    How to do that.. ? Give a like so I can get a notification when you answer.. and my app is available on playstore too.. which I made from combining four tutorials of you. 🙏

  4. Duh Santino

    Hello, my friend!

    I love your tutorials because they always work!

    I have a small question:

    – How can I save it to another directory? In this example, it is saved in the folders from which the app is mounted on the system. For example, I wanted to save to the /Downloads directory.
    How could this be made?

    Thank you very much!

  5. Giga Joules

    I'm trying to make us of this method to simply append lines of text to a file. I have put these methods in a seperate class but when I call the append method with the click of a button in an activity I get a crash and can't seem to get to the bottom of it.
    I Have the constructor which takes in the name of the file to be written to when a "Violation logger" object (in my case that is what this is called) and it returns the desired text file name as defined in strings.text and fed into said constructor.
    Seems like FOS is always null? Pls help>?
    This is my Violation_Logger Class.
    ——————————————————————————————————————————————————————————————————————————————————————————————————————
    import android.app.Application;

    import android.util.Log;

    import java.io.BufferedReader;

    import java.io.FileInputStream;

    import java.io.FileNotFoundException;

    import java.io.FileOutputStream;

    import java.io.IOException;

    import java.io.InputStreamReader;

    public class Violation_Logger extends Application {

    static String LOG_LOCATION = "LoggingError";

    public Violation_Logger(String logFileName)

    {

    LOG_LOCATION = logFileName;

    Log.d("MyDebug", "Violation Logger target is " + logFileName);

    }

    public void AppendViolation(String violation)

    {

    FileOutputStream fos = null;

    try {

    fos = openFileOutput(LOG_LOCATION, MODE_PRIVATE);

    fos.write(violation.getBytes());

    String saveLogMessage = String.format("Saving Violation: " + violation + " to " + getFilesDir() + "/" + LOG_LOCATION);

    Log.d("MyDebug", saveLogMessage);

    } catch (FileNotFoundException e) {

    // Create the File!

    // Write again?

    e.printStackTrace();

    } catch (IOException e) {

    e.printStackTrace();//We couldnt do this for some other reason.

    }finally {

    if (fos != null){

    try {

    fos.close();

    } catch (IOException e) {

    e.printStackTrace();

    }

    }

    }

    }

    public String load()

    {

    FileInputStream fis = null;

    String violationsLog = "LOG LOADING ERROR";

    try {

    fis = openFileInput(this.LOG_LOCATION);

    InputStreamReader isr = new InputStreamReader(fis);

    BufferedReader br = new BufferedReader(isr);

    StringBuilder sb = new StringBuilder();

    while ((violationsLog = br.readLine()) != null){

    sb.append(violationsLog).append("n");

    }

    } catch (FileNotFoundException e) {

    e.printStackTrace();

    } catch (IOException e) {

    e.printStackTrace();

    } finally {

    if (fis != null) {

    try {

    fis.close();

    } catch (IOException e) {

    e.printStackTrace();

    }

    }

    }

    return violationsLog;

    }

    }

  6. syed nayyar mansoor

    the only problem is how i can open that file from explorer ? or can i copy the data from that file ? actually i am loading SMS on this file but i cannot do any thing to it becuase its protected what would be the solution ? my ideas are , either output on SD card ( but not all phones have SD cards ) then how to save it to a user friendly location to be accessed later from locked phones explorers ( i can access this file through root but not other wise ) please help

Leave a Reply

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