【发布时间】:2014-08-24 00:33:52
【问题描述】:
我正在运行一个需要读取文本文件并将其保存为字符串的 java 应用程序,但我不断收到 NoSuchFileException。该文本文件位于 src 旁边的名为 assets 的文件夹中。
static String readFile(String path, Charset encoding) throws IOException {
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
这是读取我在这里找到的文件的方法。
try {
string = readFile("test.txt",Charset.defaultCharset());
} catch (IOException e) {
e.printStackTrace();
}
我尝试过“/assets/test.txt”和其他变体也无济于事。
【问题讨论】:
-
如果你运行
System.out.println(System.getProperty("user.dir"));会返回什么?你的文件与用户目录路径的相对路径是什么? -
-
它进入项目工作区并且 test.txt 在 bin 中,所以我将路径更改为 bin/test.txt 并解决了它。谢谢
标签: java eclipse file nosuchfileexception