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 位置保存文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)