我不明白为什么我不断收到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)
因为文件没有项目的目录中存在的异常被抛出。 –