我不明白爲什麼我不斷收到FileNotFoundException? 我通過Eclipse使用Java,並試圖讀取一個整數的file.txt,我想將它們加在一起來測試它們,但不斷得到異常。爲什麼我不斷收到FileNotFoundException?
我已經將文件導入到eclipse中。 這是我的代碼和我得到的異常消息。
public class FileRead {
//fields
private Scanner s;
private int[] arr;
/**
* @throws FileNotFoundException
*
*/
public FileRead() throws FileNotFoundException{
s = new Scanner(new File("lab1_data.txt"));
arr = new int[10000000];
for(int i = 0;i < arr.length;i++){
arr[i] = s.nextInt();
}
}
public int addArr(){
int a = 0;
for(int x: arr){
a = a + x;
}
return a;
}
/**
* @param args
*/
public static void main(String[] args) {
FileRead r = null;
try {
r = new FileRead();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
r.addArr();
}
}
java.io.FileNotFoundException: lab1_data.txt (The system cannot find the
file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at FileRead.<init>(FileRead.java:22)
at FileRead.main(FileRead.java:44)
Exception in thread "main" java.lang.NullPointerException
at FileRead.main(FileRead.java:48)
因爲文件沒有項目的目錄中存在的異常被拋出。 –