我有一个读取文本文件的程序。文本文件包含如下数据:
编号:1 I.D:xyz
1 abc 3.2 4.0389 0.3815 2 def 24
25.2395 CTO 0.8719 4 ghi 2 1.9139 0.2855
编号:2 I.D:abc
1 lfr 3.2 4.0389 0.3815 2 tyu 24
25.2395 CTO 0.8719 4 lui 2 1.9139 0.2855
我需要我的程序来读取文件并存储从“Number”开始的每个部分,并以最后一行结束,其中数字“4”位于该行的开头并将其存储到数组中。 使用以下RegEx“[\ s \ t \ n] +”分割这些行 换句话说,数组应如下所示:
Array [0] = Number Array [1] =:Array [2] = 1 Array [3] = I.D Array [4] =: 数组[5] = xyz数组[6] = 1数组[7] = abc数组[8] = 3.2数组[9] = 4.0389 数组[10] = 0.3815数组[11] = 2数组[12] = def数组[13] = 24数组[14] = 25.2395 Array [15] = CTO Array [16] = 0.8719 Array [17] = 4 Array [18] = ghi Array [19] = 2 Array [20] = 1.9139 Array [21] = 0.2855
再次循环迭代:
Array [0] = Number Array [1] =:Array [2] = 2 Array [3] = I.D Array [4] =: 数组[5] = lfr数组[6] = 1数组[7] = abc数组[8] = 3.2数组[9] = 4.0389 数组[10] = 0.3815数组[11] = 2数组[12] = tyu数组[13] = 24数组[14] = 25.2395 Array [15] = CTO Array [16] = 0.8719 Array [17] = 4 Array [18] = lui Array [19] = 2 Array [20] = 1.9139 Array [21] = 0.2855
我尝试过不同的方法解决这个问题,但我不知道如何这样做。
问题究竟是: 我需要比较不同行中的值。如果程序读取4行并将其存储到同一个数组中,这将更容易。这样我知道我正在比较什么数组元素。我只知道如何逐行读取数组,并不总是能满足我的需要,因为当程序进入文件的下一行时迭代数组我无法比较数据中的值前一行或正在读取的行之后的值。
这就是我现在所拥有的:
try {
BufferedReader br = new BufferedReader(new FileReader("/Users/yesmil_03/Desktop/NetBeansProjects//123.txt"));
String line = null;
String[] value = null;
while ((line = br.readLine()) != null) {
if (line.isEmpty() || line.trim().equals("") || line.trim().equals("\n")) {
continue;
}
value = line.split("[\\s\\t\\n]+");
int index = 0;
for (String s : value) {
System.out.println("values[" + index + "] = " + s);
index++;
}
}
br.close();
} catch (IOException e) {
System.out.println("Error!");
}
这是我的代码的输出:
values[0] = Number
values[1] = :
values[2] = 1
values[3] = I.D
values[4] = :
values[5] = xyz
values[0] = 1
values[1] = abc
values[2] = 3.2
values[3] = 4.0389
values[4] = 0.3815
values[5] = 2
values[6] = def
values[7] = 24
values[0] =
values[1] = 25.2395
values[2] = CTO
values[3] = 0.8719
values[4] = 4
values[5] = ghi
values[6] = 2
values[7] = 1.9139
values[8] = 0.2855
values[0] = Number
values[1] = :
values[2] = 2
values[3] = I.D
values[4] = :
values[5] = abc
values[0] = 1
values[1] = lfr
values[2] = 3.2
values[3] = 4.0389
values[4] = 0.3815
values[5] = 2
values[6] = tyu
values[7] = 24
values[0] =
values[1] = 25.2395
values[2] = CTO
values[3] = 0.8719
values[4] = 4
values[5] = lui
values[6] = 2
values[7] = 1.9139
values[8] = 0.2855
非常感谢任何帮助!
答案 0 :(得分:0)
根据你想要的你应该在每1个或更多个空格分开你的行,所以:
value = line.split("[\\s\\t\\n]+");
应改为:
value = line.split("\\s+");
另外,您没有存储字符串。这段代码生成您请求的输出
String[] array = new String[200];
int index = 0;
try {
BufferedReader br = new BufferedReader(new FileReader("./src/cenas.txt"));
String line = null;
String[] value = null;
while ((line = br.readLine()) != null) {
if (line.isEmpty() || line.trim().equals("")
|| line.trim().equals("\n")) {
continue;
}
value = line.split("\\s+");
for (int i = 0; i < value.length; i++) {
array[index++] = value[i];
System.out.println("value[" + index + "] = " + value[i]);
}
}
br.close();
} catch (IOException e) {
System.out.println("Error!");
}
输出:
值[1] =数值[2] =:值[3] = 1值[4] = ID值[5] =:值[6] = xyz值[7] = 1值[8] = abc值[9] = 3.2值[10] = 4.0389值[11] = 0.3815值[12] = 2值[13] = def值[14] = 24值[15] = 25.2395值[16] = CTO值[17] = 0.8719值[18] = 4值[19] = ghi值[20] = 2值[21] = 1.9139值[22] = 0.2855值[23] =数值[24] =:值[25 ] = 2值[26] = ID值[27] =:值[28] = abc值[29] = 1值[30] = lfr值[31] = 3.2值[32] = 4.0389值[33] = 0.3815值[34] = 2值[35] = tyu值[36] = 24值[37] = 25.2395值[38] = CTO值[39] = 0.8719值[40] = 4值[41] = lui值[42] = 2值[43] = 1.9139值[44] = 0.2855