Ich verstehe nicht, warum ich immer eine FileNotFoundException bekomme? Ich benutze Java durch Eclipse und versuche, eine Datei.txt von Ganzzahlen zu lesen, und ich möchte sie zusammenfügen, um sie zu testen, aber immer noch die Ausnahme erhalten.Warum bekomme ich immer eine FileNotFoundException?
Ich habe die Datei in Eclipse importiert. Hier ist mein Code und die Ausnahme Nachrichten, die ich bekomme.
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)
Die Ausnahme wird ausgelöst, da die Datei in Ihrem Projektverzeichnis existiert nicht. –