Android FileOutputStream 位置保存文件

Android FileOutputStream location save file(Android FileOutputStream 位置保存文件)

本文介绍了Android FileOutputStream 位置保存文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序将用户输入的数据保存到文件(内部存储)中,并在启动时加载此文件并显示内容.我想知道:在哪里可以找到我的文件 (data.txt)?此外,如果我在加载文件时输入Hello",然后输入World",我会在同一行看到HelloWorld",但我希望将Hello"和World"打印在两个不同的行上.

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.

保存文件:

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());
    }
}

用于加载文件:

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;
}

谢谢.

[更新]

我在每次输入后都插入了 ":

I've inserted " " after every input:

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

但是当我打印我的文件时,它们总是在同一行.

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

推荐答案

在哪里可以找到我的文件 (data.txt)?

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

您可以使用 YourActivity.this.getFilesDir().getAbsolutePath() 找到由 openFileOutput 创建的目录的路径.

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

但我想将Hello"和World"打印在两行不同的行中.

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

在文件中写入一个单词后使用line.separator,例如:

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

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

或者你也可以使用 replaceAll 将字符串分成多行:

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

String separator = System.getProperty("line.separator");
data=data.replaceAll(" ",separator);

这篇关于Android FileOutputStream 位置保存文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Android FileOutputStream 位置保存文件

基础教程推荐