在嘗試讀取文件時,我一直收到相同的錯誤。該文件存在於該目錄中,我做錯了什麼?java.io.FileNotFoundException - 目錄中存在文件
package test;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class MenuSample{
public static void main(String[] args) {
File f = new File("C:/Users/Joe/Documents/workspace/ArtificialLifeFX/res/latest.txt");
Scanner scanner = null;
try {
scanner = new Scanner(f);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
scanner.useDelimiter("\r\n");
}
}
我得到以下錯誤:
java.io.FileNotFoundException: C:\Users\Joe\Documents\workspace\ArtificialLifeFX\res\latest.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at test.MenuSample.main(MenuSample.java:16)
Exception in thread "main" java.lang.NullPointerException
at test.MenuSample.main(MenuSample.java:21)
原諒我,如果我太天真,我是新來的Java。我在Windows 7上運行Eclipse Luna。
幾個想法... Java是文件名區分大小寫,而Windows不是,所以確保大小寫匹配。 – Jeremy 2015-01-09 19:46:57
另外,字符串中的分隔符是正斜槓,而窗口是反斜槓。我不確定Java是否足夠聰明來解決這個問題。 – Jeremy 2015-01-09 19:47:27
爲了測試路徑是否真的存在,你可以嘗試打開一個shell並輸入'cd C:\ Users \ Joe \ Documents \ workspace \ ArtificialLifeFX \ res \' – wassgren 2015-01-09 19:52:46