设为首页 加入收藏

TOP

Java实时监控日志文件并输出(二)
2017-06-20 10:22:52 】 浏览:594
Tags:Java 实时 监控 日志 文件 输出
cheduledThreadPool(1); 
        exec.scheduleWithFixedDelay(new Runnable(){ 
            public void run() { 
                try { 
                    //获得变化部分的 
                    randomFile.seek(lastTimeFileSize); 
                    String tmp = ""; 
                    while( (tmp = randomFile.readLine())!= null) { 
                        System.out.println(new String(tmp.getBytes("ISO8859-1"))); 
                    } 
                    lastTimeFileSize = randomFile.length(); 
                } catch (IOException e) { 
                    throw new RuntimeException(e); 
                } 
            } 
        }, 0, 1, TimeUnit.SECONDS); 
    } 
     
    public static void main(String[] args) throws Exception { 
        LogView view = new LogView(); 
        final File tmpLogFile = new File("mock.log"); 
        view.realtimeShowLog(tmpLogFile); 
    } 
 


执行LogSvr类,LogSvr类会启动一个线程,每5秒钟向mock.log日志文件写一次数据,然后再执行LogView类,LogView每隔1秒钟读一次,如果数据有变化则输出变化的部分.


结果输出:


2010-06-19 17:25:54  99bill test !
2010-06-19 17:25:59  99bill test !
2010-06-19 17:26:04  99bill test !
2010-06-19 17:26:09  99bill test !
2010-06-19 17:26:14  99bill test !
2010-06-19 17:26:19  99bill test !


PS:


  代码修改过, 有朋友下载了我的代码,说如果是中文会乱码,将日志输出类的第30行的代码 
  System.out.println(tmp)改成 System.out.println(new String(tmp.getBytes("ISO8859-1"))),就会正常显示中文.


 


首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Go语言生成uuid 下一篇我是如何开始踏上 bash 脚本编程..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目