我正在使用扫描仪库来构建文件阅读器,我遵循了库中的大纲,它编译得很好,但是当我运行它时,我得到一个 FileNotFoundException :text.txt(系统找不到指定的文件) 。该文件与 .java 文件位于同一文件夹中,但它仍然显示它不存在。
下面是代码,如果对我有帮助就太好了。
import java.util.*;
import java.io.*;
class Conjecture {
public static void main(String[] args) throws IOException {
Scanner scanner = null;
try {
scanner = new Scanner(new BufferedReader(new FileReader("text.txt")));
while (scanner.hasNext()) {
System.out.println(scanner.next());
}
} finally {
if (scanner != null) {
scanner.close();
System.out.println("done");
}
}
}
}
最佳答案
您的文件需要位于运行时 JVM 的工作目录中。如果您对此不确定,可以执行以下操作:
File file = new File(".");
System.out.println(file.getAbsolutePath());
关于java - 系统找不到该文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21146756/