Java 正在尝试部分读取此文件。读一些部分,跳过一些
我试图读取一个文件,我想读取文件的某些部分并跳过某些部分,直到到达文件的结尾。这就是我所拥有的,但我没有得到我想要的结果。其思想是通读所有行,检查它是否以“CH”或“CL”开头,如果不移动到下一行,直到文件结束,并存储所有符合条件的行Java 正在尝试部分读取此文件。读一些部分,跳过一些,java,Java,我试图读取一个文件,我想读取文件的某些部分并跳过某些部分,直到到达文件的结尾。这就是我所拥有的,但我没有得到我想要的结果。其思想是通读所有行,检查它是否以“CH”或“CL”开头,如果不移动到下一行,直到文件结束,并存储所有符合条件的行 new BufferedReader (new FileReader ("c:\\demo5.properties\\")); while(( br.readLine()) != null ){ if (br.readLine().sta
new BufferedReader (new FileReader ("c:\\demo5.properties\\"));
while(( br.readLine()) != null ){
if (br.readLine().startsWith("CH") || br.readLine().startsWith("CL")) {
buf = new StringBuffer();
buf.append( br.readLine());
while (br.readLine()!= null && !br.readLine().startsWith("#") && !br.readLine().contains("claimID")){
buf.append( br.readLine()).append( "\n" );
}
z = buf.toString();
System.out.println(z);
s3+= z;
}
br.readLine();
文件格式
CH.field
CH.field
CH.field
CH.field
CH.field
#CH.field
#CH.field
#CH.field
#CH.field
*********
**
*********
CL1.field
CL1.field
CL1.field
CL1.field
CL1.field
*****************
#Result.CL1.field
#Result.CL1.field
#Result.CL1.field
Result.CL1.field
*********
CL4.field
CL4.field
CL4.field
#CL4.field
#CL4.field
#CL4.field
***********
#Result.CL4
#Result.CL4
#Result.CL4
#Result.CL
Result.CL4
CL5.field
CL5.field
CL5.field
#CL5.field
#CL5.field
输出
CH.fieldCH.field
CL1.field#Result.CL1.field
CL4.field
CL4.field
#CL4.1字段
看看这个循环:
while (br.readLine()!= null
&& !br.readLine().startsWith("#")
&& !br.readLine().contains("CH.field.claimID")){
buf.append( br.readLine()).append( "\n" );
}
您正在调用readLine()
4次。这将读四行不同的文字。你想要的是:
String line;
while ((line = br.readLine()) != null &&
!line.startsWith('#') &&
!line.contains("CH.field.claimID")) {
buf.append(line).append("\n");
}
至少,我认为这是你想要的——我发现描述有点混乱。无论如何,这将读取每一行,当它到达文件的结尾或以“#”开头的行或包含“CH.field.claimID”的行时停止。。。但是建立一个StringBuffer
,包含该点之前的所有行。看看这个循环:
while (br.readLine()!= null
&& !br.readLine().startsWith("#")
&& !br.readLine().contains("CH.field.claimID")){
buf.append( br.readLine()).append( "\n" );
}
您正在调用readLine()
4次。这将读四行不同的文字。你想要的是:
String line;
while ((line = br.readLine()) != null &&
!line.startsWith('#') &&
!line.contains("CH.field.claimID")) {
buf.append(line).append("\n");
}
至少,我认为这是你想要的——我发现描述有点混乱。无论如何,这将读取每一行,当它到达文件的结尾或以“#”开头的行或包含“CH.field.claimID”的行时停止。。。但是建立一个StringBuffer
,包含该点之前的所有行。调用readLine()将读取以“\n”结尾的一行,然后文件指针将指向下一行(或者更准确地说,它将指向“\n”之后的任何行)
例如
在readLine()之前:
文件指针-->这是第一行\n
-----------------这是第二行\n
在readLine()之后:
------------------这是第一行\n
文件指针-->这是第二行\n
您多次调用readLine(),认为它引用了同一行。这就是您出错的地方。对readLine()的调用将读取以“\n”结尾的一行,然后文件指针将指向下一行(或者更准确地说,它将指向“\n”之后的任何内容)
例如
在readLine()之前:
文件指针-->这是第一行\n
-----------------这是第二行\n
在readLine()之后:
------------------这是第一行\n
文件指针-->这是第二行\n
您多次调用readLine(),认为它引用了同一行。这就是你的错误所在。从你的问题中我发现你想要存储所有以“CH”或“CL”开头的行,而忽略其余的行 所有br.readline调用都将该文件读取器指针移动到下一行 格式对我来说很奇怪,也许可以尝试以下方法:
String input = "";
buf = new StringBuffer();
new BufferedReader (new FileReader ("c:\\demo\\TestIL_Cross_000005.properties\\"));
// read in the line to "input" if it exists
while((input = br.readLine()) != null ){
// check for starting string match
if (input.startsWith("CH") || input.startsWith("CL")) {
// append to whatever data struct here since it matches
buf.append(input + "\n");
// print "input" here for testing
}
}
根据您的问题,我得出结论,您希望存储所有以“CH”或“CL”开头的行,而忽略其余的行 所有br.readline调用都将该文件读取器指针移动到下一行 格式对我来说很奇怪,也许可以尝试以下方法:
String input = "";
buf = new StringBuffer();
new BufferedReader (new FileReader ("c:\\demo\\TestIL_Cross_000005.properties\\"));
// read in the line to "input" if it exists
while((input = br.readLine()) != null ){
// check for starting string match
if (input.startsWith("CH") || input.startsWith("CL")) {
// append to whatever data struct here since it matches
buf.append(input + "\n");
// print "input" here for testing
}
}
这是您在代码中犯的一个非常基本的错误。必须将readLine()方法返回的数据存储在字符串变量中,并将其用于进一步的操作。每次调用readLine()方法时,如果希望对收到的字符串执行操作,则每次都会获取新数据,而以前的数据会丢失 代码的修改版本:
new BufferedReader (new FileReader ("c:\\demo\\TestIL_Cross_000005.properties\\"));
String line = null;
while(( line= br.readLine()) != null ){
if (line.startsWith("CH") || v.startsWith("CL")) {
buf = new StringBuffer();
buf.append(line);
while (line!= null && !line.startsWith("#") && !line.contains("CH.field.claimID")){
buf.append( line).append( "\n" );
}
z = buf.toString();
System.out.println(z);
s3+= z;
}
}
在这里,我刚刚从字符串引用中的readLine()方法收集了数据,并用于执行不同的检查。这是您在代码中犯的一个非常基本的错误。必须将readLine()方法返回的数据存储在字符串变量中,并将其用于进一步的操作。每次调用readLine()方法时,如果希望对收到的字符串执行操作,则每次都会获取新数据,而以前的数据会丢失 代码的修改版本:
new BufferedReader (new FileReader ("c:\\demo\\TestIL_Cross_000005.properties\\"));
String line = null;
while(( line= br.readLine()) != null ){
if (line.startsWith("CH") || v.startsWith("CL")) {
buf = new StringBuffer();
buf.append(line);
while (line!= null && !line.startsWith("#") && !line.contains("CH.field.claimID")){
buf.append( line).append( "\n" );
}
z = buf.toString();
System.out.println(z);
s3+= z;
}
}
在这里,我刚刚从字符串引用中的readLine()方法收集了数据,并用于执行不同的检查。在验证该行是否以CH或CL开头后,我仍然想检查该行是否包含特定字符串,如果它确实跳过该行并移到下一行,我的错误。我很高兴Jon能帮你解决这个问题。在验证行是否以CH或CL开头后,我仍然想检查行是否包含某些字符串,如果它确实跳过该行并移动到下一行,我的错误。我很高兴乔恩能帮你解决这个问题。