今天解決坐下來與我的教授了堅實的30分鐘。有效。希望這可以幫助別人。的Java:之前,我們想出了「嘗試」行之前創建的「掃描儀的FileInput」掃描器無法讀取.txt文件
廣泛的研究之後,我一直無法找出原因掃描儀不PIC了「hurricane.txt」我把它保存爲項目爲.txt。我正在使用NetBeans。任何幫助表示讚賞!
// Openning hurricane data file
try{
System.out.println("Openning hurricane data file...");
Scanner fileInput = new Scanner(new File("hurricane.txt"));
}
catch(FileNotFoundException e){
System.err.println("FileNotFoundException: " + e.getMessage());
return;
}
System.out.println("File opened successfully...");
System.out.println("Reading file...");
這是輸出我得到:
run:
Openning hurricane data file...
FileNotFoundException: hurricane.txt (The system cannot find the file specified)
BUILD SUCCESSFUL (total time: 1 second)
我想你沒有你的類路徑hurricane.txt。嘗試添加它'的System.out.println的類路徑 – dpassy 2015-02-23 21:51:52
後的結果(新文件(「hurricane.txt」)getAbsolutePath());',看看你真的有文件在這個位置。 – Pshemo 2015-02-23 21:52:50
對此的唯一可能的解釋是該名稱不相同或文件不在正確的目錄中。下面的答案可以幫助你。哦,不要忘了在完成之後關閉流。您可能只想使用Java提供的Try-With-Resource語句。 – Nikita 2015-02-23 22:20:04