Java追踪日志tail -f
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>
使用静态方法
public static void main(String[] args) {
final Tailer tailer = Tailer.create(new File("C:\\Users\\zhaojingbo\\Desktop\\22"), new TailerListenerAdapter() {
//处理日志
@Override
public void handle(String line) {
System.out.println("日志---" + line);
}
});
// 线程等待
final CountDownLatch countDownLatch = new CountDownLatch(5);
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
使用Executor
public static void main(String[] args) {
final Tailer tailer = new Tailer(new File("C:\\Users\\zhaojingbo\\Desktop\\22"), new TailerListenerAdapter() {
@Override
public void handle(String line) {
System.out.println("日志---" + line);
}
});
Executor executor = new Executor() {
public void execute(Runnable command) {
command.run();
}
};
executor.execute(tailer);
}
使用thread
public static void main(String[] args) {
final Tailer tailer = new Tailer(new File("C:\\Users\\zhaojingbo\\Desktop\\22"), new TailerListenerAdapter() {
@Override
public void handle(String line) {
System.out.println("日志---" + line);
}
});
Thread thread = new Thread(tailer);
thread.setDaemon(true); // optional
thread.start();
}
Loading...