Question

I have a crawler program which logs some files. Sometimes on the server, some error happens and the crawler creates massive log files which are somehow impossible to parse. For that reason, I wanted to create a simple program which reads about 1000 characters at the end of the log file and shows the messages to me (even if the crawler is still writing to that file). This will help me solve the problem without closing the crawler.

Was it helpful?

Solution

Using a RandomAccessFile to seek, then read your bytes out.

File file = new File("DemoRandomAccessFile.out");
RandomAccessFile raf = new RandomAccessFile(file, "r");

// Seek to the end of file
raf.seek(file.length() - n);
// Read it out.
raf.read(yourbyteArray, 0, n);

OTHER TIPS

There is a handy command line tool for this already on your computer. tail -c 1000 would do what you are asking for. tail -n 10 printing out the last 10 lines may be even more useful.

Check the file length, lets say 1Mb

Open for read with RandomAccessFile

Seek to position to 1024*1024-1000

read 1000 bytes

upvote :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top