Getting FileNotFoundException even though file exists and is spelled correctly [duplicate]

二次信任 提交于 2019-12-29 08:02:33

问题


I'm creating a small program that will read a text file, which contains a lot of randomly generated numbers, and produce statistics such as mean, median, and mode. I have created the text file and made sure the name is exactly the same when declared as a new file.

Yes, the file is in the same folder as the class files.

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();


    }
}

}

Error line reads as follows:

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)

回答1:


How about adding:

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

Print this out. It will tell you what the current working directory is. Then you should be able to see why it isn't finding the file.

Rather than allowing your code to throw, you could check to allow yourself to do something if the file isn't found:

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

Please run the below and paste the output:

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());



回答2:


The problem is you have specified only a relative file path and don't know what the "current directory" of your java app is.

Add this code and everything all will be clear:

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

By examining the absolute path you will find that the file is not is the current directory.

The other approach is to specify the absolute file path when creating the File object:

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

btw, try to stick to naming conventions: name your variables with a leading lowercase letter, ie gradeList not GradeList



来源:https://stackoverflow.com/questions/10791986/getting-filenotfoundexception-even-though-file-exists-and-is-spelled-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!