Java Apache Commons IO Tailer 示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22987170/
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-08-13 19:47:27  来源:igfitidea点击:

Apache Commons IO Tailer example

javaapache-commons-io

提问by user2435860

I am working on a monitoring program that reads the /var/log/auth.log file. I am using Apache Commons IO Tailerclass to read the file in real time. To get started, I wanted to test the real-time reading part on a simple file, and manually enter some code in the console line. Here is my code:

我正在开发一个读取 /var/log/auth.log 文件的监控程序。我正在使用 Apache Commons IOTailer类实时读取文件。首先,我想在一个简单的文件上测试实时读取部分,并在控制台行中手动输入一些代码。这是我的代码:

public class Main {
    public static void main(String[] args) {
        TailerListener listener = new MyListener();
        Tailer tailer = Tailer.create(new File("log.txt"), listener, 500);
        while(true) {

        }
    }
}

public class MyListener extends TailerListenerAdapter {
    @Override
    public void handle(String line) {
        System.out.println(line);
    }
}

And from the terminal : sudo echo "Hello" >> log.txtThe problem is when I try to write manually something in the file, it does not print it in the console. I tried to find a concrete example of usage of Tailer class, but no luck. What am I doing wrong here?

从终端:sudo echo "Hello" >> log.txt问题是当我尝试在文件中手动编写某些内容时,它不会在控制台中打印出来。我试图找到一个使用 Tailer 类的具体例子,但没有运气。我在这里做错了什么?

回答by Duncan Jones

Based on my testing, Tailerwill only print a line when you've added a newline to the file. So try sudo echo "Hello\n" >> log.txt

根据我的测试,Tailer当您向文件添加换行符时,只会打印一行。所以试试sudo echo "Hello\n" >> log.txt

Also note that if you call create, you start a thread but have no handle on it. Hence why you had to have a while/true loop.

另请注意,如果您调用create,则会启动一个线程,但无法处理它。因此,为什么你必须有一个 while/true 循环。

You could try this instead:

你可以试试这个:

public static void main(String[] args) {
    TailerListener listener = new MyListener();
    Tailer tailer = new Tailer(new File("log.txt"), listener, 500);        
    tailer.run();
}

回答by Duncan Jones

Your code should work. For me, this does works as expected.

您的代码应该可以工作。对我来说,这确实按预期工作。

package de.lhorn.stackoverflowplayground;

import java.io.File;
import org.apache.commons.io.input.Tailer;
import org.apache.commons.io.input.TailerListenerAdapter;

public class App {

    private static final int SLEEP = 500;

    public static void main(String[] args) throws Exception {
        App app = new App();
        app.run();
    }

    private void run() throws InterruptedException {
        MyListener listener = new MyListener();
        Tailer tailer = Tailer.create(new File("/tmp/log.txt"), listener, SLEEP);
        while (true) {
            Thread.sleep(SLEEP);
        }
    }

    public class MyListener extends TailerListenerAdapter {

        @Override
        public void handle(String line) {
            System.out.println(line);
        }

    }
}

回答by APISonar

Using Tailer.create:

使用 Tailer.create:

public void tail(TailerListener listener) {

        File file = new File("log.txt")

        Tailer tailer = Tailer.create(file, listener, 500);
        tailer.run();
    }

http://apisonar.com/java-examples/org.apache.commons.io.input.Tailer.create.html

http://apisonar.com/java-examples/org.apache.commons.io.input.Tailer.create.html