我使用Eclipse來編譯和運行我的java代碼。線程「主」異常java.io.FileNotFoundException:錯誤
這是我得到的錯誤。
Exception in thread "main" java.io.FileNotFoundException: file.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 helloworld.main(helloworld.java:9)
這裏是我的代碼
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class helloworld {
public static void main(String[] args) throws IOException {
Scanner KB = new Scanner(new File("file.txt"));
while (KB.hasNext()) {
String line = KB.nextLine();
System.out.println(line);
}
}
}
FILE.TXT
我已經在我的項目創造了同一個文件夾file.txt的。
您的文件是否直接在您的項目文件夾下? –
它在SCR下,我也把一個放在bin下面,因爲scr沒有工作。 – Mowgli
嘗試打印'新的文件(「file.txt」)。exists()'是否產生'true'?如果沒有,嘗試打印'新的文件(「file.txt」)。getAbsoluteFile()'這是你所期望的? – amit