我有一个包含以下几行的文本文件:
150004|2012|12|15|0|0|3|0|0|-3.2411|83.9962|156.3321|1.1785|205.3125|2.0599
150004|2012|12|15|0|10|3|0|0|-3.4206|85.9575|150.4877|1.4142|226.7578|2.4276
150004|2012|12|15|0|20|3|0|0|-2.2696|86.2675|149.3848|2.1553|225.7031|3.4387
每个'|' 符号表示它有一列。我必须从“|”内的每一行中提取信息 迹象。当我尝试以下代码时:
File filer = new File("C:\\Users\\Ali Y. Akgul\\Desktop\\150004_15122012_G.txt");
try (BufferedReader reader = new BufferedReader(new FileReader(filer))) {
while (true) {
String line = reader.readLine();
if (line == null) {
break;
}
String[] fields = line.split("|");
// process fields here
for(int i=0;i<=fields.length;i++){
System.out.println(fields[i]);
}
}
}
}
它给了我:
1
5
0
0
0
4
|
2
0
1
2
|
1
2
|
1
5
|
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 76
0
|
0
|
3
|
0
|
0
|
-
3
.
2
4
at testenv.TestEnv.main(TestEnv.java:31)
1
1
|
8
3
.
9
9
6
2
|
1
5
6
.
3
3
2
1
|
1
.
1
7
8
5
|
2
0
5
.
3
1
2
5
|
2
.
0
5
9
9
Java Result: 1
我怎样才能正确解析它?