I have an assignment for my CS class where it says to read a file with several test scores and asks me to sum and average them. While summing and averaging is easy, I am having problems with the file reading. The instructor said to use this syntax
Scanner scores = new Scanner(new File("scores.dat"));
However, this throws a FileNotFoundException
, but I have checked over and over again to see if the file exists in the current folder, and after that, I figured that it had to do something with the permissions. I changed the permissions for read and write for everyone, but it still did not work and it still keeps throwing the error. Does anyone have any idea why this may be occurring?
EDIT: It was actually pointing to a directory up, however, I have fixed that problem. Now file.exists()
returns true
, but when I try to put it in the Scanner
, it throws the FileNotFoundException
Here is all my 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()); Scanner scan = new Scanner(file); } }
If we find the error message there is no such file or directory; we can remove that exception by re-verifying the code and checking whether the given file is available in the given directory or not.
This exception is thrown during a failed attempt to open the file denoted by a specified pathname. Also, this exception can be thrown when an application tries to open a file for writing, but the file is read-only, or the permissions of the file do not allow the file to be read by any application.
This is likely because you're inside either the bin or src folders when you run the java binary. The working directory in this case is whichever directory the command prompt is currently inside. If, for example, you go into the /src/ directory, say javac *.
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 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With