出错代码
(这段代码位于Src_exp2_3.java中)
public static String getValue(String key) throws IOException{
Properties pro = new Properties();
// here
FileReader in = new FileReader("pro.txt");
pro.load(in);
in.close();
return pro.getProperty(key);
}
报错如下
Exception in thread "main" java.io.FileNotFoundException: pro.txt (系统找不到指定的文件。)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at java.io.FileReader.<init>(FileReader.java:58)
at exp2.Src_exp2_3.getValue(Src_exp2_3.java:36)
at exp2.Src_exp2_3.main(Src_exp2_3.java:23)
项目结构(部分)
出错原因
以该路径表达式(“pro.txt”)读取的文件需要直接处于项目根目录下,而不能处于任何子目录下;
有两种解决方法,如下:
- 将pro.txt移动到Design_Mode_Code根目录下
- 将路径表达式修改为:“src\\exp2\\pro.txt”
(PS: 如果将路径表达式修改为"\\src\\exp2\\pro.txt",也会报错)
参考链接
可以参考StackOverflow的这段解答:StackOverflow上的解答