我正在尝试使用 java 获取文件的第一行,但我不确定它为什么不工作或为什么我得到我得到的错误。这是我第一次在java中尝试这个。
错误
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 16
at getSum.main(getSum.java:33)
代码
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.File;
import java.io.FileNotFoundException;
public class getSum {
public static void main(String[] args) {
File file = new File("path/InputArray.txt");
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
String line = null;
try{
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
while(dis.available() != 0){
line = dis.readLine();
}
}catch (FileNotFoundException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
String[] splitLine = line.split("\\s+");
int[] numbers = new int[splitLine.length];
for(int i = 0; i< line.length(); i++){
try{
numbers[i] = Integer.parseInt(splitLine[i]);
}catch(NumberFormatException nfe){};
}
int amount = 0;
System.out.print(numbers.length);
/*amount = sumArray(0,numbers.length,numbers.length,numbers);
System.out.print("Total: " + amount);*/
}