Android FileOutputStream location save file
Asked Answered
S

2

16

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.

Salita answered 7/1, 2015 at 12:2 Comment(1)
how can i set new path for saving file?Salita
G
14

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);
Grapher answered 7/1, 2015 at 12:17 Comment(7)
my path is /data/data/mypackage but if i go there is empty..maybe it's necessary my device is rooted to see it.. String separator = System.getProperty("line.separator"); doesn't work..words are still in same lineSalita
@xXJohnRamboXx: are you sure when you are reading data from file then adding new line ?Disclaimer
@xXJohnRamboXx: try it as :: stringBuilder.append(receiveString);stringBuilder.append("\n");Disclaimer
i print my contet file into a textview component and the words aren't in separate linesSalita
@xXJohnRamboXx: to view file try it using DDMSDisclaimer
ok adding stringBuilder.append("\n"); now works perfectly! Last question: how can i save my file into my dowload folder?Salita
Let us continue this discussion in chat.Disclaimer
F
4

I guess you can find it here: data/data/[your package name]/... and regarding writing in two separate lines just add "\n" whenever you need to go to another line.

Fribble answered 7/1, 2015 at 12:8 Comment(2)
my data/data is empty..i've just added "\n" after each words but nothing :/Salita
If you want to write data in your sdcard then once carefully read this tutorial : chrisrisner.com/…Fribble

© 2022 - 2024 — McMap. All rights reserved.