【发布时间】:2015-06-06 08:43:50
【问题描述】:
我有这种方法,它只从文件中读取数字并将其存储到两个不同的数组列表中,每行一个。它将行存储为字符串,因此它只有一个索引。
如何从文件中读取并分隔所有数字,以便获得索引 0-14,因为我需要它们稍后进行计算?
该文件包含以下内容:
vot1:8,8,9,1,7,6,8,8,9,5,6,8,7,9,8
vot2:7,8,8,8,9,4,7,8,10,7,8,9,8,8,9
我的代码是这样的:
public void readFileTest(String path) throws FileNotFoundException {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(path));
String line = null;
int x=0;
while ((line = br.readLine()) != null) {
x++;
if (x<2){
line = line.substring(line.indexOf(":") + 1);
numbers.addAll(Arrays.asList(line.split(",")));
}else{
line = line.substring(line.indexOf(":") + 1);
numbers1.addAll(Arrays.asList(line.split(",")));
}
}
} catch (FileNotFoundException ex) {
System.err.println(ex);
} catch (IOException ex) {
System.err.println(ex);
} finally {
try {
br.close();
} catch (Exception ex) {
System.err.println(ex);
}
}
for (Object c : numbers) {
System.out.print(c);
}
System.out.println();
for (Object c : numbers1) {
System.out.print(c);
}
}
【问题讨论】: