Android FileOutputStream location save file(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 位置保存文件
基础教程推荐
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01