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

2

5

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)
Expectorant answered 29/5, 2012 at 1:6 Comment(15)
Would you please include the information about the IDE, and the directory structure?Doublecheck
The IDE is Intellij IDEA, directory structure is GradeStats->src which contains the text file, and other 3 classes including the one shown here.Expectorant
The file may be in the same directory as the class files, but that is not normally the current directory. Try to do a system.out.println(GradeList.getAbsolutePath())Pineapple
@Pineapple I tried doing that, and found the file to be outside the src folder, but it still did not working when changing the path in the File("location here"). Still getting the same file not found exception.Expectorant
Then use the full pathname to the file. It should be obvious that you're looking in the wrong place for the file.Ingar
Work up the path one directory at a time, testing for each directory's existence. You'll find your problem.Albertson
@HovercraftFullOfEels C:\Users\myName\IdeaProjects\GradeStats\GradeList.txt is the full file path I used.Expectorant
You used backslashes? That shouldn't compile. Use forward slashes instead.Ingar
Using forward slashes didn't change anything. :(Expectorant
Show your latest code with your use of the fully qualified file name.Ingar
What happened to your file's ".txt" extension? Double check, triple check, quadruple check that everything is spelled AND capitalized exactly as it is on disk.Ingar
@HovercraftFullOfEels It did not make any difference adding a .txt to the end of it. I've already tried that with every other suggestion.Expectorant
You could always read it as a resource if its with the class files. BufferedReader bf = new BufferedReader(new InputStreamReader(GradeStats.class.getResourceAsStream("GradeList.txt")));Ingar
Didn't work either. Maybe it is on my end. Will it compile successfully with you all?Expectorant
Your code says "GradeList" and your exception message says "GradeList.txt". Ergo you are not running that code.Disarmament
O
11

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());
Oxyacetylene answered 29/5, 2012 at 1:20 Comment(10)
I tried this and it wouldn't compile, still gives FileNotFoundException.Expectorant
Did you print out curDir? What did it say? Where is GradeList located?Oxyacetylene
I've added four lines of code to the bottom of my answer. Please run and paste output hereOxyacetylene
printing out curDir resulted in C:\Users\Casi\IdeaProjects\GradeStats. The GradeList file is at \GradeStats\src but when using system.out.println(GradeList.getAbsolutePath()) it prints the same path as the curDir.Expectorant
Right so if you put: File GradeList = new File("C:\Users\Casi\IdeaProjects\GradeStats\GradeList.txt"); It should find it. Is that where windows explorer shows it as being?Oxyacetylene
If the file is at GradeStats\src, you should be able to put either: src/GradeList.txt or C:\Users\Casi\IdeaProjects\GradeStats\src\GradeList.txt both should workOxyacetylene
The four lines at the end compiles: Current sys dir: C:\Users\Casi\IdeaProjects\GradeStats Current abs dir: C:\Users\Casi\IdeaProjects\GradeStats\GradeList.txtExpectorant
Right so if I get this right, the file is physically located at: C:\Users\Casi\IdeaProjects\GradeStats\src\GradeList.txt?? If thats correct then put: File GradeList = new File("C:\Users\Casi\IdeaProjects\GradeStats\src\GradeList.txt"); and it will workOxyacetylene
That fixed it! Somehow...it didn't work earlier and I did the exact thing but I guess it works now. Phew. I'm relatively new to Java and programming and general and I tend to get stuck on the smallest thingsExpectorant
Excellent... glad it works for youOxyacetylene
C
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 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

Chancellorship answered 29/5, 2012 at 1:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.