我有一个 .txt 文件,格式为一个整数后跟一个 float ,它们在每一行中由空格字符分隔。我想从每一行读取 float ,然后把它放在一个数组中。
到目前为止,这是我的代码,但是当我运行它时,它给了我一个 ArrayIndexOutOfBoundsException
因为我猜它永远不会创建第二个值:
BufferedReader reader = null;
try{
reader = new BufferedReader(new InputStreamReader(new FileInputStream(
new File(root.getAbsolutePath().toString()+"/samplefile.txt"))));
String line = null;
String[] numbers = null;
int i = 0;
value.clear();
while((line = reader.readLine()) != null){
numbers = line.split("/\\s/");
value.add(Float.valueOf(numbers[1].trim()));
}
mTextview5.setText(String.valueOf(value.get(1)));
}catch(IOException e){
e.printStackTrace();
}
那么我怎样才能转到第二个值呢?
最佳答案
尝试:
numbers = line.split("\\s+");
关于java - 从一行中读取 float 抛出 ArrayIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36914306/