简体   繁体   English

Java如何在特定字符串之后找到特定行?

[英]Java How to find a specific line after a specific string?

I got a text from a BufferedReader and I need to get a specific line after a specific string 我从BufferedReader获得了文本,我需要在特定字符串后获取特定行

This is the text for example: 这是文本,例如:

<img src="https://example.com/image1.jpg">
<img src="https://example.com/image2.jpg">
<img src="https://example.com/image3.jpg">
<img src="https://example.com/image4.jpg">
<all>
<img src="https://example.com/image9.jpg">
<img src="https://example.com/image11.jpg">
<img src="https://example.com/image15.jpg">

example with linebreak 换行示例

<img src="https://example.com/image1.jpg">
<img src="https://example.com/image2.jpg">
<img src="https://example.com/image3.jpg">
<img src="https://example.com/image4.jpg">
<all>

<img src="https://example.com/image9.jpg">
<img src="https://example.com/image11.jpg">
<img src="https://example.com/image15.jpg">

example with other character 其他角色的例子

<img src="https://example.com/image1.jpg">
<img src="https://example.com/image2.jpg">
<img src="https://example.com/image3.jpg">
<img src="https://example.com/image4.jpg">
<all>
UTUYGBLK
<img src="https://example.com/image9.jpg">
<img src="https://example.com/image11.jpg">
<img src="https://example.com/image15.jpg">

I only want the img src line after 'all' which cont and end loop after that. 我只希望在“ all”之后的img src行,然后继续并结束循环。 the image number are random so I cant possible hit any of those. 图片编号是随机的,因此我无法打中任何一个。

currently my code is as follows. 目前我的代码如下。

while ((line = rd.readLine()) != null) {

  if (line.contains("all"))  {    
    line = rd.readLine();   
    System.out.println(line);
  }
 }

but if there is a linebreak after the 'all' i will get a null return... or funny character UTUYGBLK 但是如果在'all'之后有换行符,我将得到一个null回报...或有趣的字符UTUYGBLK

You will need to loop after the element then until the result is not null. 您将需要在元素之后循环,直到结果不为null为止。 The code below should protect you against reading lines after end of file and also stop after the first line is shown after is read. 下面的代码应防止在文件结束后读取行,并且在读取后显示第一行后也应停止。

boolean loop = true;
// global loop
while ((line = rd.readLine()) != null && loop) {
  // line with "all" in it is found?
  if(line.contains("all"))  {    
    // Continue going through the input until end of input
    while((line = rd.readLine()) != null && loop) {  
      // When the line is not empty you have found the line after all!
      if(!line.isEmpty()) {
        System.out.println(line);
        // to stop the loops
        loop = false; 
      }
    }
  }
}

Then just skip the blank lines. 然后,只需跳过空白行即可。

while ((line = rd.readLine()) != null) {

    if (line.contains("all"))  {
        while ((line = rd.readLine()) != null && line.trim().isEmpty());
        System.out.println(line);
        break;
    }
}
String line = "";
while (!line.contains("all"))
    line = rd.readLine();
line = rd.readLine();
while ("".equals(line.trim()))
    line = rd.readLine();
return line;
//or System.out.println(line);

This will: 这将:

  1. Set line to the empty string. line设置为空字符串。
  2. Read lines until it gets one containing all . 读取行,直到得到包含all行的行。
  3. Read the next line. 阅读下一行。
  4. Read lines until it gets a non-empty one. 读取行,直到获得非空行为止。
  5. Return the last thing it read. 返回它阅读的最后一件事。

This operates on the assumption that there really will be a line containing all , and that there really will be a non-blank somewhere after it. 这是基于这样的假设,即实际上将有一行包含all ,并且在它之后的某处确实将有一个非空白。

You can go into a second loop to check for nulls. 您可以进入第二个循环以检查是否为空。

while ((line = rd.readLine()) != null) {

        if (line.contains("all"))  {    
            while ((line = rd.readLine()) == null || line.isEmpty()) {
                  //keep looping while line is null

                  //check if it is no longer ready to avoid infinite loop
                  if (!rd.ready()) {break;}
            }
            //line should not be null here
            System.out.println(line);
       }

Or something to that affect. 或有什么影响。

If you use contains() if you have an image with a name containing 'all' you will have incorrect results. 如果使用contains()(如果您的图像名称包含“ all”,则结果将不正确)。 Better use equals() after trimming the line. 修剪线后最好使用equals()。 So the code will look like this: 因此,代码将如下所示:

while ((line = rd.readLine()) != null ) {
    if (line.trim().equals("<all>"))  {
        while ((line = rd.readLine()).isEmpty());
        System.out.println(line);
        break;
    }
}

You can use the method 您可以使用方法

line.isEmpty()

to handle that case 处理这种情况

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

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