How to read the tail of a large file?
My application writes to a log file. I want to read the end (say last 100 lines) of the logfile and display it on a web page. I dont want to read the entire log file as everything apart from the last 100 lines is irrelevant. Is there a way to do this? The only thing i can think of is to do this on a linux command prompt "tail logfile.txt > storehere.txt " and then read storehere.txt. Not ideal. Is there a way i can read the last 100 lines or better still have a continous stream of the last 100 lines of the log file?
[531 byte] By [
sanh2005a] at [2007-11-26 13:02:46]

This question has been ask here before; but, I am not sure that a good solution was found.
Assuming the file does not have fixed length records (then it would be easy), the only thing I can think of is:
Use RandomAccessFile to read the bytes in the file backwards and store them until you have processed 100 lines. In reality, you would probably not want to read one byte at a time but read "chucks" of the file into a buffer and process those bytes. Similar to how BufferedInputStream works only getting the bytes in reverse order.
I.E.
Create a class that returns a byte of the file in reverse order. This class would fill a buffer with a chuck as needed.
One big issue with this idea is if you are reading Unicode characters - multiple bytes would need to be consumed to pass back a character and since not all Unicode characters are 2-bytes, this could get messy.
It would be easier and faster to read the last X bytes instead of the last X lines. Unless you really care that you need 100 lines, I would use bytes. You can estimate how many bytes you need to go back from the end fo the file (using RandomAccessFile).