【发布时间】:2014-05-23 13:51:15
【问题描述】:
代码:
import java.io.*;
import java.util.Scanner;
public class Driver {
private int colorStrength;
private String color;
public static void main(String[] args) throws IOException {
String line, file = "strength.txt";
File openFile = new File(file);
Scanner inFile = new Scanner(openFile);
while (inFile.hasNext()) {
line = inFile.nextLine();
System.out.println(line);
}
inFile.close();
}
}
这是我正在为一个类编写的程序的一小部分(我知道这两个私有属性尚未使用)但是当我尝试使用 strength.txt 文件运行它时,我收到以下错误:
例外:
Exception in thread "main" java.io.FileNotFoundException: strength.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 Driver.main(Driver.java:14)
如果有 Eclipse 的任何人可以帮助我解决这个问题,将不胜感激!
【问题讨论】:
-
strength.txt是否与Driver.class位于同一位置? -
是的。我把它保存在程序的 src 文件夹以及 Project 文件夹中
-
不是。 @PM77-1 要求您检查它是否与 Driver.class 位于同一位置。如果您查看
bin目录,您只会看到 Driver.class ,除非您在任一答案中进行以下提到的更改。 -
和Driver.class一起在bin目录下还是不行
-
好的。检查拼写。
标签: java eclipse filenotfoundexception