我在这里发布了一个类似的问题:Read from a file containing integers - java但无法得到体面的回复。从文件(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”是否可以读取它? – 2012-07-07 09:43:43
“这很奇怪,因为该文件在项目的文件夹中” - 当您指定完全限定的文件名时,项目的文件夹不相关。 – 2012-07-07 09:44:23
与正斜杠相同的错误。 @JonSkeet谢谢你。我知道,因为起初我只写了temperature.txt而不是完整的路径。 – stud91 2012-07-07 09:47:06