【问题标题】:Getting FileNotFoundException even though file exists and is spelled correctly [duplicate]即使文件存在并且拼写正确,也会出现 FileNotFoundException [重复]
【发布时间】:2012-05-29 01:06:05
【问题描述】:

我正在创建一个小程序,它将读取一个文本文件,该文件包含大量随机生成的数字,并生成平均值、中位数和众数等统计数据。我已经创建了文本文件,并确保在声明为新文件时名称完全相同。

是的,该文件与类文件位于同一文件夹中。

public class GradeStats {
public static void main(String[] args){
    ListCreator lc = new ListCreator(); //create ListCreator object
    lc.getGrades(); //start the grade listing process
    try{
        File gradeList = new File("C:/Users/Casi/IdeaProjects/GradeStats/GradeList");
        FileReader fr = new FileReader(gradeList); 

        BufferedReader bf = new BufferedReader(fr);       

        String line;

        while ((line = bf.readLine()) != null){
            System.out.println(line);
        }
        bf.close();
    }catch(Exception ex){
        ex.printStackTrace();


    }
}

}

错误行内容如下:

java.io.FileNotFoundException: GradeList.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileReader.<init>(FileReader.java:72)
    at ListCreator.getGrades(ListCreator.java:17)
    at GradeStats.main(GradeStats.java:11)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

【问题讨论】:

  • 能否请您提供有关 IDE 和目录结构的信息?
  • 该文件可能与类文件在同一目录中,但通常不是当前目录。尝试做一个 system.out.println(GradeList.getAbsolutePath())
  • 然后使用文件的完整路径名。很明显,您在错误的位置查找文件。
  • 您文件的“.txt”扩展名发生了什么变化?仔细检查、三重检查、四重检查,确保所有内容的拼写和大写与磁盘上的完全一致。
  • 您的代码显示“GradeList”,而您的异常消息显示“GradeList.txt”。因此,您没有运行该代码。

标签: java filenotfoundexception


【解决方案1】:

如何添加:

String curDir = System.getProperty("user.dir");

打印出来。它会告诉你当前的工作目录是什么。然后你应该能够看到为什么它没有找到文件。

如果找不到文件,您可以检查以允许自己执行某些操作,而不是允许您的代码抛出:

File GradeList = new File("GradeList.txt");
if(!GradeList.exists()) {
    System.out.println("Failed to find file");
   //do something
}

请运行以下命令并粘贴输出:

String curDir = System.getProperty("user.dir");
File GradeList = new File("GradeList.txt");
System.out.println("Current sys dir: " + curDir);
System.out.println("Current abs dir: " + GradeList.getAbsolutePath());

【讨论】:

  • 我试过了,它不会编译,仍然给出 FileNotFoundException。
  • 你打印出 curDir 了吗?它说了什么? GradeList 在哪里?
  • 我在答案的底部添加了四行代码。请在此处运行并粘贴输出
  • 打印出 curDir 导致 C:\Users\Casi\IdeaProjects\GradeStats。 GradeList 文件位于 \GradeStats\src 但使用 system.out.println(GradeList.getAbsolutePath()) 时,它会打印与 curDir 相同的路径。
  • 对,如果你输入: File GradeList = new File("C:\Users\Casi\IdeaProjects\GradeStats\GradeList.txt");它应该找到它。这是 Windows 资源管理器显示它的地方吗?
【解决方案2】:

问题是你只指定了一个相对文件路径,不知道你的java应用的“当前目录”是什么。

添加此代码,一切都会清楚:

File gradeList = new File("GradeList.txt");
if (!gradeList.exists()) {
    throw new FileNotFoundException("Failed to find file: " + 
        gradeList.getAbsolutePath());
}

通过检查绝对路径你会发现文件不是是当前目录。

另一种做法是在创建File对象时指定绝对文件路径:

File gradeList = new File("/somedir/somesubdir/GradeList.txt");

顺便说一句,尽量坚持命名约定:使用前导小写字母命名变量,即gradeList 而不是GradeList

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-18
    • 2015-09-27
    • 1970-01-01
    • 2014-02-17
    相关资源
    最近更新 更多