Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android FileOutputStream location save file

I have an app that saves into a file (internal storage) data input by the user and at startup it loads this file and shows the contents. I would like to know: where can I find my file (data.txt)? In addition, if I input "Hello" and then "World" when I load the file, I see "HelloWorld" in the same line but I want "Hello" and "World" printed on two different lines.

For saving file:

public void writeToFile(String data) {
    try {
        FileOutputStream fou = openFileOutput("data.txt", MODE_APPEND);
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fou);
        outputStreamWriter.write(data);
        outputStreamWriter.close();
    }
    catch (IOException e) {
        Log.e("Exception", "File write failed: " + e.toString());
    }
}

For loading file:

public String readFromFile() {

    String ret = "";

    try {
        InputStream inputStream = openFileInput("data.txt");

        if ( inputStream != null ) {
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String receiveString = "";
            StringBuilder stringBuilder = new StringBuilder();

            while ( (receiveString = bufferedReader.readLine()) != null ) {
                stringBuilder.append(receiveString);
            }

            inputStream.close();
            ret = stringBuilder.toString();
        }

    }
    catch (FileNotFoundException e) {
        Log.e("login activity", "File not found: " + e.toString());
    } catch (IOException e) {
        Log.e("login activity", "Can not read file: " + e.toString());
    }

    return ret;
}

Thanks.

[UPDATE]

I've inserted "\n" after every input:

user = (EditText) v.findViewById(R.id.username);
writeToFile(user.getText().toString() + "\n");

But when I print my file, they are always on the same line.

like image 624
xXJohnRamboXx Avatar asked Jan 07 '15 12:01

xXJohnRamboXx


People also ask

How to get file path from FileOutputStream in Android?

String absolutePath = myFile. getAbsolutePath();

What is FileOutputStream in Android?

FileOutputStream(String name) Creates a file output stream to write to the file with the specified name. FileOutputStream(String name, boolean append) Creates a file output stream to write to the file with the specified name.

What is difference between OutputStream and FileOutputStream?

Outputstream is an abstract class whereas FileOutputStream is the child class. It's possible that you could use Outputstream to just write in bytes for a prexisting file instead of outputting a file.

What is FileOutputStream in Kotlin?

FileOutputStream(name: String!) Creates a file output stream to write to the file with the specified name. A new FileDescriptor object is created to represent this file connection.


1 Answers

Where can I find my file (data.txt)?

You can find the path of the directory, which is created by openFileOutput, by using YourActivity.this.getFilesDir().getAbsolutePath().

But I want "Hello" and "World" printed in two different lines.

Use line.separator after writing a word in a file, for example:

String separator = System.getProperty("line.separator");
outputStreamWriter.write(data);
outputStreamWriter.append(separator);
...

Or you can also use replaceAll to break String into multiple lines:

String separator = System.getProperty("line.separator");
data=data.replaceAll(" ",separator);
like image 69
ρяσѕρєя K Avatar answered Sep 21 '22 12:09

ρяσѕρєя K