-
FileReader errors
hello all,
I am trying to build and run the code below on netbeans IDE, and for some reason I am getting a error message which I cannot solve. this is exactly how the code appears in multiple tutorials, which is why I am confused. the error message I am getting is:
run:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
at fileiotest.Main.main(Main.java:25)
Java Result: 1
The file that I want to read is definitely in the right place, on my desktop. Here is the code itself:
package fileiotest;
import java.io.*;
import java.util.*;
import java.io.FileReader;
public class Main {
public static void main(String[] args) {
FileReader fr = new FileReader ("/home/iochinome/Desktop/wdbc.data");
}
}
and strangely enough, it throws the same error message even when i replace the line
FileReader fr = new FileReader ("/home/iochinome/Desktop/wdbc.data");
with
File f = new File("/home/iochinome/Desktop/wdbc.data");
FileReader fr = new FileReader ("/home/jay/Desktop/wdbc.data");
Thanks!!!!
-
Re: FileReader errors
It generally helps if you read the error message before trying to solve the problem. The message says "Uncompilable source code - unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown".
This has nothing to do with where the file is, the problem is the code can't be compiled. The message goes on to say the named exception must be caught or thrown. This tells you what needs to be done to your code to get it to compile. ie you have to catch and handle the FileNotFoundException or you declare the main method as throwing the exception. For example:
Code:
package fileiotest;
import java.io.*;
import java.util.*;
import java.io.FileReader;
public class Main throws FileNotFoundException {
public static void main(String[] args) {
FileReader fr = new FileReader ("/home/iochinome/Desktop/wdbc.data");
}
}