简体   繁体   中英

java eclipse- fileReader throws FileNotFoundException though <file>.exists() returns true

when i attempt to create a BufferedReader using a text file in my workspace the FileReader in the "new bufferedreader statement throws a filenotfoundexception. yet .exists() and .canRead() both return true for the file

public static void main(String[] args) {
    File fighter0 = new File("resources/fighter0.txt");
    //BufferedReader reader = new BufferedReader(new FileReader(fighter0));
    System.out.println(fighter0.exists());
    System.out.println(fighter0.canRead());
}

here's the code

true
true

and the output

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
Unhandled exception type FileNotFoundException

at main.Core.main(Core.java:21)

and the exception that's thrown when i uncomment the bufferedreader line

i found a closed thread similar to this one, but couldn't find any clear answer in it.

This is a compiler error, not a runtime failure:

Unhandled exception type FileNotFoundException

The compiler is stating that that exception must be caught by the client code. It can be corrected by implementing a try/catch around the use of FileReader or by stating that main() throws FileNotFoundException .

Research checked and unchecked exceptions.

Your error is compilation error. It says that you have not handled the FileNotFoundException exception. Try public static void main(String[] args) throws Exception

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM