Java实时读取日志文件

在实习的公司碰到一个古怪的需求:在一台服务器上写日志文件,每当日志文件写到一定大小时,比如是1G,会将这个日志文件改名成另一个名字,并新建一个与原文件名相同的日志文件,再往这个新建的日志文件里写数据;要求写一个程序能实时地读取日志文件中的内容,并且不能影响写操作与重命名操作。

RandomAccessFile类中seek方法可以从指定位置读取文件,可以用来实现文件实时读取。JDK文档对RandomAccessFile的介绍

Instances of this class support both reading and writing to a random access file. A random access file behaves like a large array of bytes stored in the file system. There is a kind of cursor, or index into the implied array, called the file pointer; input operations read bytes starting at the file pointer and advance the file pointer past the bytes read. If the random access file is created in read/write mode, then output operations are also available; output operations write bytes starting at the file pointer and advance the file pointer past the bytes written. Output operations that write past the current end of the implied array cause the array to be extended.

在每一次读取后,close一下就不会影响重命名操作了。

编码实现

代码参考Java实时监控日志文件并输出LogTailer.java

写日志文件,每秒写200条记录,并且记录写的时间

import Java.io.File; import java.io.IOException; import java.io.RandomAccessFile; import java.text.SimpleDateFormat; import java.util.Date; public class LogReader implements Runnable { private File logFile = null; private long lastTimeFileSize = 0; // 上次文件大小 private static SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); public LogReader(File logFile) { this.logFile = logFile; lastTimeFileSize = logFile.length(); } /** * 实时输出日志信息 */ public void run() { while (true) { try { long len = logFile.length(); if (len < lastTimeFileSize) { System.out.println("Log file was reset. Restarting logging from start of file."); lastTimeFileSize = len; } else if(len > lastTimeFileSize) { RandomAccessFile randomFile = new RandomAccessFile(logFile, "r"); randomFile.seek(lastTimeFileSize); String tmp = null; while ((tmp = randomFile.readLine()) != null) { System.out.println(dateFormat.format(new Date()) + "\t" + tmp); } lastTimeFileSize = randomFile.length(); randomFile.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { Thread.sleep(50); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }

实时读取日志文件,每隔1秒读一次

import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; import java.text.SimpleDateFormat; import java.util.Date; public class LogReader implements Runnable { private File logFile = null; private long lastTimeFileSize = 0; // 上次文件大小 private static SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); public LogReader(File logFile) { this.logFile = logFile; lastTimeFileSize = logFile.length(); } /** * 实时输出日志信息 */ public void run() { while (true) { try { RandomAccessFile randomFile = new RandomAccessFile(logFile, "r"); randomFile.seek(lastTimeFileSize); String tmp = null; while ((tmp = randomFile.readLine()) != null) { System.out.println(dateFormat.format(new Date()) + "\t" + tmp); } lastTimeFileSize = randomFile.length(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/9af309da958648a007c4946feb20eb5d.html