Code:
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 的人可以帮助我解决这个问题,我将不胜感激!
您已经使用过relative与您的项目执行相关的文件路径。
如果您想这样做,只需将strength.txt
文件在基目录您的项目。就像这样:
或者,您可以参考absolute
系统上的文件路径。例如,使用:
Windows:
C:/dev/myproject/strength.txt
Mac/Unix:
/Users/username/dev/strength.txt
(或者任何完整路径)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)