Java追踪日志tail -f

走着路睡觉
  • java
小于 1 分钟

<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();
    }
上次编辑于:
贡献者: zhaojingbo
Loading...