简体   繁体   English

Java使用两个循环读取文本文件

[英]Java reading text file using two loops

I'm away from my computer right now but I had an idea and I really wanna know if it'll work. 我现在离开了我的电脑,但我有一个想法,我真的想知道它是否有效。

Would this rough code work for getting groups of lines out of a text file (using BufferedReader br ): 这个粗略的代码是否适用于从文本文件中获取行组(使用BufferedReader br ):

String line;
BufferedReader br = ....;
List<String> list = new ArrayList<String>();

while(line = br.readline() != null){
    if(line.equals("Group1"){
        while(line = br.readline() != "}"){
            list.add(line);
        }
    }
}

Here would be the text file: 这将是文本文件:

Group1
one
two
three
}
Group2
....
}

Try to use single loop like this: 尝试使用这样的单循环:

boolean isGroup=false;
while(line = br.readline() != null){
    if(line.equals("Group1"){
      isGroup=true;
    }
    if(line.equals("}") && isGroup)
      isGroup=false;
    if(isGroup){
      //read line and check whether it is null or not
      list.add(line);
    }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM