【发布时间】:2012-07-07 09:41:03
【问题描述】:
我在这里发布了一个类似的问题:Read from a file containing integers - java 但无法得到一个体面的答复。
现在我已经编写了这个新代码,它只读取一个文件并输出结果。
每当我尝试从文件中读取时,都会收到 FileNotFoundException。代码如下:
import java.io.*;
public class second {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
File f = new File("C:\\Users\\Haroon\\workspace\\ppp\\temperature.txt");
FileReader file = new FileReader(f);
BufferedReader buf = new BufferedReader(file);
String s = null;
while((s = buf.readLine()) != null){
System.out.println(s);
}
}
}
这很奇怪,因为该文件位于项目的文件夹中。 任何帮助将不胜感激。
【问题讨论】:
-
如果你尝试:“C:/Users/Haroon/workspace/ppp/temperature.txt”你能读懂吗?
-
“这很奇怪,因为文件在项目的文件夹中” - 当您指定完全限定的文件名时,项目的文件夹是无关紧要的。
-
与正斜杠相同的错误。 @JonSkeet 对此表示感谢。我知道,因为起初我只写了 temperature.txt 而不是完整路径。
-
仔细检查您的文件名。我的猜测是某处有错字。
标签: java file-io filenotfoundexception