What could be causing Java to throw a FileNotFoundException even when the file exists?

I have an assignment for my CS class where I need to read a file containing test scores, sum them, and calculate their average.

Summing and averaging are straightforward, but I’m running into an issue while reading the file. My instructor suggested using this syntax:

Scanner scores = new Scanner(new File("scores.dat"));

However, when I run the program, Java throws a java FileNotFoundException, even though I have checked multiple times that the file exists in the correct folder. At first, I thought it was a permission issue, so I changed the file’s read and write permissions for everyone, but the error persists.

After some debugging, I realized that the program was originally pointing to the wrong directory. I fixed that issue, and now file.exists() returns true, but Java still throws FileNotFoundException when I try to pass the file into Scanner.

Here’s my full code:

import java.util.Scanner;
import java.io.*;

public class readInt {
    public static void main(String args[]) {
        File file = new File("lines.txt");
        System.out.println(file.exists()); // Returns true, but Scanner still fails
        Scanner scan = new Scanner(file);
    }
}

What could be causing java FileNotFoundException, and how can I resolve it?

Hi Dhara,

It sounds like you’re dealing with a really frustrating issue—when Java throws a FileNotFoundException even though the file clearly exists. Don’t worry! This happens more often than you’d think.

Even though file.exists() returns true, Java might still be looking in the wrong directory when you pass it to Scanner.

Try printing the absolute path of the file to confirm where Java is actually looking:

File file = new File("lines.txt");
System.out.println("Absolute Path: " + file.getAbsolutePath());
System.out.println("File exists? " + file.exists());
Scanner scan = new Scanner(file);

This will show you the exact location where Java is searching for lines.txt. If it’s not where you expect, try using the full path instead:

File file = new File("C:\\Users\\YourName\\Documents\\lines.txt");

or for macOS/Linux:

File file = new File("/Users/YourName/Documents/lines.txt");

This should resolve the java FileNotFoundException if it’s a directory issue.

Even if a file exists, Java may not have permission to open it. Here’s what you can do:

Manually check permissions – Right-click the file, go to Properties > Security, and ensure your user has Read permissions.

Check if the file is open in another program – Sometimes, another program locks the file, preventing Java from accessing it.

Run your IDE as Administrator – If you’re using an IDE like IntelliJ or Eclipse, running it with admin privileges can help resolve permission issues.

If you’re still getting java FileNotFoundException, try catching the exception to get more details:

try (Scanner scan = new Scanner(new File("lines.txt"))) {
    while (scan.hasNextLine()) {
        System.out.println(scan.nextLine());
    }
} catch (FileNotFoundException e) {
    System.out.println("File not found! Check the file path and permissions.");
    e.printStackTrace();
}

This way, you’ll get a more detailed error message, which might give you a clue as to why Java can’t find the file.