I’m very confused on getting basic file reading to work with Java. Lots of mixed signals.
I’ve tried it a couple different ways and I consistently get a not found exception each time. I’ve checked with a file object set to the current path to print the current directory and I am indeed in the directory the file I’m trying to open is in. The permissions are set so everyone can read. I’m not sure what is going on:
BufferedReader infixLines = new BufferedReader ( new FileReader ( "input.infix" ));
This is the line that throws the error, consequently each consecutive line using infixLines also throws an error.
I’ve tried it using FileInputStream as well and get the same kind of error.
That being said simply doing
File file = new File("input.infix");
if ( file.exists() )
System.out.println( "Exists" );
DOES work.
Very confused.
EDIT: (Stacktrace?)
ParseInfix.java:13: unreported exception java.io.FileNotFoundException; must be
BufferedReader infixLines = new BufferedReader(new FileReader (n
^
ParseInfix.java:15: unreported exception java.io.IOException; must be caught or
while ( ( line = infixLines.readLine()) != null )
The exception trace is saying that your code…
Could possibly throw a
FileNotFoundExceptionor anIOExceptionif the file doesn’t exist, so it wants you to do something to handle this possibility.The simplest way is to wrap your file-reading code in a
try-catchblock like this…The exception isn’t saying that the file can’t be found, it is just saying that if the file doesn’t exist, what is your code going to do to handle the situation.
Ultimately in the
catchblock you would want to do something more than justSystem.out.println(). For example, in a GUI program, you might show a popup message to tell the user that the file doesn’t exist.