即使文件存在,FileNotFoundException也是如此 [英] FileNotFoundException even when the file is there

查看:127
本文介绍了即使文件存在,FileNotFoundException也是如此的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public StormAnalysis(){
    try {       
        fScanner = new Scanner(new File("tracks1949to2010_epa.txt"));
        while(fScanner.hasNextLine()){
            System.out.println(fScanner.nextLine());
        }
    } catch (FileNotFoundException e) {
        System.out.println("File not found. Try placing the tracks1949to2010_epa.txt in the same folder as StormAnalysis.java");    
        e.printStackTrace();
    }

}

以上是我的代码(和我还有一个错误的图像:
http: //folk.uio.no/arnabkd/test/images/error-code-task.jpg

The above is my code (and I also have an image of the error : http://folk.uio.no/arnabkd/test/images/error-code-task.jpg

如您所见,txt文件位于与StormAnalysis.java文件相同的文件夹。此外,如果我将文件路径更改为weather.dat(这是另一个任务/问题),代码也可以工作。

As you can see, the txt file is in the same folder as the StormAnalysis.java file. In addition, the code works if I change the file path to "weather.dat" (which was given as another task/problem).

任何想法都将受到赞赏!

Any ideas will be appreciated!

推荐答案

该文件不在那里。如果它不是抛出异常: - )

可能的罪魁祸首是工作目录与预期不同(即, 当前工作目录不包含具有该名称的文件)。这可以通过使用文件的绝对路径并观察它是否正确加载来轻松验证。

The likely culprit is the working directory differs from what is expected (that is, the current working directory does not contain a file with that name). This can be trivially verified with using the file's absolute path and observing that it is loaded correctly.

或者,要查找当前目录:

Alternatively, to find the current directory:

String cwd = new File(".").getAbsolutePath();

快乐编码。

这篇关于即使文件存在,FileNotFoundException也是如此的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆