public StormAnalysis(){
try {
fScanner = new Scanner(new File("tracks1949to2010_epa.txt"));
while(fScanner.hasNextLine()){
System.out.println(fScanner.nextLine());
}
} catch (FileNotFoundException e) {
System.out.println("File not found. Try placing the tracks1949to2010_epa.txt in the same folder as StormAnalysis.java");
e.printStackTrace();
}
}
以上是我的代碼(我也有錯誤的圖像: http://folk.uio.no/arnabkd/test/images/error-code-task.jpgFileNotFoundException異常,即使該文件是有
正如你所看到的,txt文件是在同一文件夾中StormAnalysis.java文件。此外,代碼工作,如果我改變(這是給出另一個任務/問題)的文件路徑爲「weather.dat」。
任何想法可以理解的!
@Arnab,由於您使用的是File的構造函數的相對路徑,因此計算的路徑將始終相對於java/javaw可執行文件的工作目錄。您需要修改對構造函數的調用,以包含您的文件所在的任何子目錄。 – 2011-06-01 07:05:49