【发布时间】:2012-11-27 20:17:25
【问题描述】:
我正在使用 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
我在项目的同一文件夹中创建了 file.txt。
【问题讨论】:
-
你的文件是直接在你的项目文件夹下吗?
-
它在SCR下,我也在bin下放了一个,因为scr不起作用。
-
尝试打印
new File("file.txt").exists()是否产生true?如果没有,请尝试打印new File("file.txt").getAbsoluteFile()是否符合您的预期? -
它在你执行它的同一目录中寻找文件 file.txt。如果是来自 IDE,请检查工作目录的设置。
-
试着把它和你的 .class 文件放在同一个文件夹下。
标签: java eclipse file-io exception-handling java.util.scanner