【发布时间】:2016-08-02 21:59:30
【问题描述】:
我设置了尝试/捕获此错误的方法。我的问题是,即使我在同一个包中明确创建 Trivia.txt,它也会捕获 Trivia.txt 的 fileNotFoundException。我无法弄清楚为什么找不到该文件。我环顾四周寻找问题的答案,但没有运气。无论如何,这是我的代码
public static void readFile(){
try{
File file = new File("Trivia.txt");
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
while((line = br.readLine()) != null){
System.out.println(line);
}
br.close();
}
catch(FileNotFoundException e){
System.out.println("file not found");
System.out.println();
}
catch(IOException e){
System.out.println("error reading file");
}
}
这里的代码只是TextHandler类的一个方法,被WindowComp类(完全不相关的类)静态调用。包是 mainPackage,它包含 main() 和 WindowComp() 和 textHandler() 以及 Triva.Txt
【问题讨论】:
-
“在包中创建”是什么意思?请显示您的目录结构。
标签: java filenotfoundexception