java 连续读取附加到日志文件的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2238369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 20:14:40  来源:igfitidea点击:

Continually read the lines being appended to a log file

java

提问by ant

Concerning my previous question, I found out that maven can't really output jboss console. So I thought I'd like to make workaround it. Here is the deal:

关于我之前的问题,我发现 maven 无法真正输出 jboss 控制台。所以我想我想解决它。这是交易:

While jboss is running, it writes console logs into server.log file, so I'm trying to retrieve the data as it comes in, because every few seconds the file is changes/updated by jboss I've encountered some difficulties so I need help.

当 jboss 运行时,它将控制台日志写入 server.log 文件,所以我试图检索数据,因为每隔几秒钟,jboss 就会更改/更新文件,我遇到了一些困难,所以我需要帮助。

What I actually need is:

我真正需要的是:

  1. read file server.log
  2. when server.log is changed with adding few more lines output the change
  1. 读取文件 server.log
  2. 当 server.log 更改并添加更多行时输出更改

Here is the code so far I got, there is a problem with it, it runs indefinitely and it starts every time from the beginning of the file, I'd like it to continue printing just the new lines from server.log. Hope it makes some sense here is the code:

这是到目前为止我得到的代码,它有问题,它无限期地运行并且每次都从文件的开头开始,我希望它继续只打印 server.log 中的新行。希望这里的代码有意义:

import java.io.*;


class FileRead 
{
   public static void main(String args[])
  {
      try{
   for(;;){ //run indefinitely
    // Open the file 
    FileInputStream fstream = new FileInputStream("C:\jboss-5.1.0.GA\server\default\log\server.log");
    BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
    String strLine;
    //Read File Line By Line
    while ((strLine = br.readLine()) != null)   {
      // Print the content on the console
      System.out.println (strLine);
    }
    //Close the input stream
    in.close();
    }
  }
      catch (Exception e){//Catch exception if any
      System.err.println("Error: " + e.getMessage());
    }
  }
}

According to the Montecristo suggestion I did this :

根据 Montecristo 的建议,我这样做了:

import java.io.*;

class FileRead {
    public static void main(String args[]) {
        try {
            // Open the file that is the first
            // command line parameter
            FileInputStream fstream = new FileInputStream(
                    "C:\jboss-5.1.0.GA\server\default\log\server.log");
            BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
            String line;
            // Read File Line By Line
            while ((line = br.readLine()) != null) {
                // Print the content on the console
                line = br.readLine();
                if (line == null) {
                    Thread.sleep(1000);
                } else {
                    System.out.println(line);
                }

            }
            // Close the input stream
            in.close();

        } catch (Exception e) {// Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }
    }
}

And it still not working, it just printed the original file.. although the file changes constantly nothing happens.. nothing gets printed out except the original log file.

它仍然无法正常工作,它只是打印原始文件。

HERE IS THE SOLUTION:tnx Montecristo

这是解决方案:tnx Montecristo

import java.io.*;

class FileRead {
    public static void main(String args[]) {
        try {

            FileInputStream fstream = new FileInputStream(
                    "C:\jboss-5.1.0.GA\server\default\log\server.log");

            BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
            String line;

            while (true) {

                line = br.readLine();
                if (line == null) {
                    Thread.sleep(500);
                } else {
                    System.out.println(line);
                }

            }

        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }
    }
}

Also see :

另见:

http://vanillajava.blogspot.co.uk/2012/08/java-memes-which-refuse-to-die.html

http://vanillajava.blogspot.co.uk/2012/08/java-memes-which-refuse-to-die.html

采纳答案by Alberto Zaccagni

I don't know if you're going in the right direction but if I've understood correctly you'll find this useful: java-io-implementation-of-unix-linux-tail-f

我不知道你是否朝着正确的方向前进,但如果我理解正确,你会发现这很有用:java-io-implementation-of-unix-linux-tail-f

回答by Shilkumar Jadhav

You can use RandomAccessFile.

您可以使用 RandomAccessFile。

import java.io.IOException;
import java.io.RandomAccessFile;

public class LogFileReader {

    public static void main( String[] args ) {
       String fileName = "abc.txt";
       try {
        RandomAccessFile bufferedReader = new RandomAccessFile( fileName, "r" 
        );

        long filePointer;
        while ( true ) {
            final String string = bufferedReader.readLine();

            if ( string != null )
                System.out.println( string );
            else {
                filePointer = bufferedReader.getFilePointer();
                bufferedReader.close();
                Thread.sleep( 2500 );
                bufferedReader = new RandomAccessFile( fileName, "r" );
                bufferedReader.seek( filePointer );
            }

        }
    } catch ( IOException | InterruptedException e ) {
        e.printStackTrace();
    }

}
}