Java says FileNotFoundException but file exists
There are a number situation where a FileNotFoundException
may be thrown at runtime.
The named file does not exist. This could be for a number of reasons including:
- The pathname is simply wrong
- The pathname looks correct but is actually wrong because it contains non-printing characters (or homoglyphs) that you did not notice
- The pathname is relative, and it doesn't resolve correctly relative to the actual current directory of the running application. This typically happens because the application's current directory is not what you are expecting or assuming.
- The path to the file is is broken; e.g. a directory name of the path is incorrect, a symbolic link on the path is broken, or there is a permission problem with one of the path components.
The named file is actually a directory.
The named file cannot be opened for reading for some reason.
The good news that, the problem will inevitably be one of the above. It is just a matter of working out which. Here are some things that you can try:
Calling
file.exists()
will tell you if any file system object exists with the given name / pathname.Calling
file.isDirectory()
will test if it is a directory.Calling
file.canRead()
will test if it is a readable file.This line will tell you what the current directory is:
System.out.println(new File(".").getAbsolutePath());
This line will print out the pathname in a way that makes it easier to spot things like unexpected leading or trailing whitespace:
System.out.println("The path is '" + path + "'");
Look for unexpected spaces, line breaks, etc in the output.
It turns out that your example code has a compilation error.
I ran your code without taking care of the complaint from Netbeans, only to get the following exception message:
Exception in thread "main" java.lang.RuntimeException: Uncompilable
source code - unreported exception java.io.FileNotFoundException; must
be caught or declared to be thrown
If you change your code to the following, it will fix that problem.
public static void main(String[] args) throws FileNotFoundException {
File file = new File("scores.dat");
System.out.println(file.exists());
Scanner scan = new Scanner(file);
}
Explanation: the Scanner(File)
constructor is declared as throwing the FileNotFoundException
exception. (It happens the scanner it cannot open the file.) Now FileNotFoundException
is a checked exception. That means that a method in which the exception may be thrown must either catch the exception or declare it in the throws
clause. The above fix takes the latter approach.
Getting FileNotFoundException even though file exists and is spelled correctly
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());
fileNotFoundException but file exists
You should use
InputStream is = new URL("http://stackoverflow.com/").openStream();
instead of a FileInputStream
.
Java FileNotFoundException even though file exists
According to the documentation for FileInputStream
, "If the named file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading then a FileNotFoundException is thrown." (emphasis mine) The file may be locked or in use by another application.
What does file.canRead()
return?
Now that you've updated your question with more data, I can see that you are misinterpreting the error message. The error is that you are calling a method which throws a certain type of exception and you are not properly reporting or handling the exception. You can either add a try
/ catch
for FileNotFoundException
or add a throws
to your method declaration which states that FileNotFoundException
can be thrown.
Related Topics
Java Switch Statement: Constant Expression Required, But It Is Constant
When Exactly Is It Leak Safe to Use (Anonymous) Inner Classes
Android N Change Language Programmatically
Couldn't Install Netbeans 11.3 with Java 14 Due to Error: "Unsupported Jvm Version"
Environmental Variables Not Seen in a Cron
Org.Apache.Commons.Net.Ftp.Parser.Parserinitializationexception: Unknown Parser Type: Linux
Converting a String to Hexadecimal in Java
Illegal Pattern Character 'T' When Parsing a Date String to Java.Util.Date
What Is an Illegal Reflective Access
Concurrentmodificationexception for Arraylist
Java.Util.Nosuchelementexception: No Line Found
Why Is 08 Not a Valid Integer Literal in Java
Spring Data JPA Deleteby Query Not Working
Find Duplicate Value in Array List and Print the Duplicated Value