我設置了方法來嘗試/捕獲此錯誤。我的問題是,即使在同一個包中顯式創建Trivia.txt時,它也會捕獲Trivia.txt的fileNotFoundException。我無法弄清楚爲什麼沒有找到該文件。我做了一些四處尋找我的問題的答案,並沒有運氣。總之,這裏是我的代碼Java 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");
}
}
這裏的代碼只是一個由WindowComp級(完全無關類)靜態調用的TextHandler類的方法。該包是mainPackage,其中包含主要()和WindowComp()和textHandler()與Triva.ondxt
你是什麼意思,「在包裝中創建?」請顯示您的目錄結構。 – OldProgrammer