我正在使用Eclipse编译和运行我的java代码。
以下是我遇到的错误。
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);
}
}
}
文件.txt
我已经在项目的同一个文件夹中创建了文件.txt。
new File("file.txt").exists()
,它是否返回true
?如果没有,请尝试打印new File("file.txt").getAbsoluteFile()
,它是否符合您的预期? - amit