Tailer
Origin
Sometimes we need to start a thread to monitor the changes of a file in real time, for example, when new content is written to the file, we can print it timely. This function is very similar to the tail -f
command in Linux.
Usage
Tailer tailer = new Tailer(FileUtil.file("f:/test/test.log"), Tailer.CONSOLE_HANDLER, 2);
tailer.start();
Where Tailer.CONSOLE_HANDLER
indicates that the default output of the added content is sent to the console.
/**
* Line handler for command line printing
*
* @author looly
* @since 4.5.2
*/
public static class ConsoleLineHandler implements LineHandler {
@Override
public void handle(String line) {
Console.log(line);
}
}
We can also implement our own LineHandler
to process each line of data.
Note This method will block the current thread