简体   繁体   English

JAVA:如何解析日志文件直到找到特定行,就像 linux 中的尾部命令一样?

[英]JAVA: How to parse log file till specific line is found like a tail command in linux?

I want to parse the log file till specific line is found in the log file like a tail command.我想解析日志文件,直到在日志文件中找到特定的行,如 tail 命令。

I am using bufferreader and want to do something like this-我正在使用 bufferreader 并想做这样的事情-

Code-代码-

      BufferedReader br = new BufferedReader(new 
       FileReader("/file1.txt"));
       String line;
       boolean keepReading = true;
       while (br.readLine()!= null) {

         line= br.readLine();
         if(line.equals("string found"))
          {
                break;
          }
         else
           {
              //some stuff
           }}

I just want to wait for particular string, till then I just want to continue the loop but the condition is, may be the specific string is yet to come and I want to wait for it?我只想等待特定的字符串,直到那时我只想继续循环,但条件是,可能是特定的字符串尚未到来,我想等待它?

String comparisons should be done with the equals(String) method.字符串比较应该使用equals(String)方法。 Strings are immutable objects, meaning each string is a separate object.字符串是不可变的对象,这意味着每个字符串都是一个单独的 object。 The equals(String) method compares the content of the strings. equals(String)方法比较字符串的内容。

In Java you check string equality by:在 Java 中,您可以通过以下方式检查字符串相等性:

if(line.equals("string found")){
}

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

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