Java – Parsing a Log File for Better Performance

javaperformance

Im reading a log file in Java on a Linux box on a continual schedule of 2 minutes looking for certain messages. I store the last offset (RandomAccessFile getFilePointer) and read from it onwards when I detect LastModified has changed; is this best practice or even right?

Best Answer

Assuming the following:

  • this log file is only appended to
  • you're prepared to meet the possibility that the file no longer exists / shorter than your current offset
  • program writing to log wouldn't attempt to delete the log file (since you may be reading it at the time) or that it handles such situations well

You shouldn't have much to worry about. Just remember to close the file after reading it and to open it with "r" mode only.

Related Topic