【发布时间】:2020-04-16 21:01:39
【问题描述】:
我正在尝试查找我保存在 android studio 中的文件。我已经尝试了一切:放置完整目录,将文件放入该目录的新文件夹中,使用file.getAbsolutePath(),在之前声明文件或将文件直接放入阅读器(BufferedReader r = new BufferedReader(new FileReader("history.txt"));
代码如下:
File file = new File("Extra Files/history.txt");
BufferedReader r = new BufferedReader(new FileReader(file.getAbsolutePath()));
String scores = r.readLine();
r.close();
无论我尝试什么,它似乎都没有任何其他想法,你能不能在 android studio 中使用这样的文本文件或???
public void write_score(int ns) throws IOException {
File file = new File(context.getFilesDir(), "history.txt");
BufferedWriter w = new BufferedWriter(new FileWriter(file));
w.append(String.valueOf(ns)).append(",");
w.close();
}
【问题讨论】:
-
如果您想知道某个文件是否存在于某个路径下,请使用 File.exists()。
-
你有抛出异常吗?如果有,是哪一个?还是
scores-string 只是空的? -
new File("Extra Files/history.txt")你使用的是相对路径。 File 类需要一个完整路径。请说出您的完整路径。 -
使用
System.out.println(new File(".").getCanonicalPath().toString());显示java进程的工作目录。你的文件是相对于那个找到的。 -
@itzFlubby 我遇到了 FileNotFoundException 异常
标签: java android file android-studio filereader