Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
Sounds like an okay implementation to me. You might want to send this question off to the codereview stackexchange instead. – Spoike Jan 21 '13 at 11:19
up vote 2 down vote accepted

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.

share|improve this answer

If it works, then it's good enough. The only problems I see with this solution is two-fold:

  1. What happens if someone edits the log file? The java program will ignore these changes.
  2. What happens if the logger splits up the log file? The last offset file pointer will be invalid.

The first point is a bit of a stretch since it is a bad idea to edit a log file (as users are only supposed to append to the file). Second point is a common practice, that the log file is split in dates or at some amount of lines.

share|improve this answer
It should return a long offset, so there's no direct tie to the file, but definitely good points. – Neil Jan 21 '13 at 11:26

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.