0

当我尝试使用工作区中的文本文件创建 BufferedReader 时,“新的 bufferedreader 语句中的 FileReader 会引发 filenotfoundexception。但是 .exists() 和 .canRead() 都为该文件返回 true

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());
}

这是代码

true
true

和输出

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

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

以及当我取消注释 bufferedreader 行时引发的异常

我发现了一个与此类似的封闭线程,但在其中找不到任何明确的答案。

4

2 回答 2

3

这是编译器错误,而不是运行时故障:

未处理的异常类型 FileNotFoundException

编译器声明该异常必须由客户端代码捕获。可以通过执行 a try/catcharound the useFileReader或声明 that来纠正它main() throws FileNotFoundException

研究检查和未经检查的异常。

于 2013-04-17T09:08:47.903 回答
0

您的错误是编译错误。它说你还没有处理FileNotFoundException异常。尝试public static void main(String[] args) throws Exception

于 2013-04-17T09:10:54.210 回答