Apache Commons IO Tailer example

半城伤御伤魂 提交于 2019-11-29 03:38:29

Based on my testing, Tailer will only print a line when you've added a newline to the file. So try 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.

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();
}

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);
        }

    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!