我使用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