Edit: Some of the comments below have fixed my problem. I needed to add a try catch block to my code or to add a throws Exception to my main method. This fixed the problem.
I know this question has been asked before but I tried all the recommendations and nothing has helped. I'm trying to read from a file and I keep getting the FileNotFoundException. Here is my code:
import java.io.File;
import java.io.FileReader;
public class TextParser {
public static void main(String[] args) {
File f = new File("C:\\Users\\jonny\\Desktop\\readme.txt");
System.out.println(f.getAbsolutePath());
System.out.println(f.exists());
// FileReader file = new FileReader(f);
}
}
With the last line commented out, the code runs and I get the output: C:\Users\jonny\Desktop\readme.txt
true
However, when I uncomment the last line, I will get this:
Error:(11, 27) java: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
How can I fix this, I've clearly specified the absolute path. Also, the file is not open on my computer as I run this program.