how to save text to file in Android Studio?

0

Issue

I want to create file in which the first line will be constantly updated. actually I want to save this file to custom path, for example /storage/emulated/0/Download, but I don’t know how to do that, so now I have something like this:

public void save(){
    while(true) {
        try {
            String FILENAME = "my_file";
            String string = "" + System.currentTimeMillis();

            FileOutputStream fos = openFileOutput(FILENAME, MODE_PRIVATE);
            fos.write(string.getBytes());
        } catch (Exception e) {

            e.printStackTrace();

        }
    }
}

and this code gives me two errors

  1. Cannot resolve method ‘openFileOutput’
  2. Cannot resolve symbol ‘MODE_PRIVATE’

Solution

 FileOutputStream fos = getApplicationContext().getContextResolver().openFileOutput(FILENAME);

Before using openFileOutpout() you have to use getContextResolver() and there is no Context.MODE_PRIVATE parameter in this function

Answered By – Saurabh Dhage

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More