0

I am working with a log file of size 2GB. When heap exceeds max limit, it throws OutOfMemory Error in my log file.

I want to simulate unix command tail -20 native_stdout.log

How do I do this in JAVA?

I cannot read line by line because it has thousands of lines, it is going to take lot of time.

And file I read is streaming one from SFTP serevr.

  • possibly answered here: [stackoverflow.com/questions/2356137](http://stackoverflow.com/questions/2356137/read-large-files-in-java) – s.ijpma Jul 09 '15 at 09:19
  • 2
    Actually, answered here - http://stackoverflow.com/questions/4121678/java-read-last-n-lines-of-a-huge-file The simplest and most robust answer is the one that suggests using org.apache.commons.io.input.ReversedLinesFileReader from apache commons.io – pvg Jul 09 '15 at 09:22
  • What have you tried and what are you having trouble with? You just need to remember the last 20 lines and once you reach the end, print them out, after that you need to print any additional lines. – Peter Lawrey Jul 09 '15 at 09:22

2 Answers2

2

Apache commons has an Simple implementation of the unix "tail -f" functionality

Tailer.html

More Information

Community
  • 1
  • 1
Garry
  • 4,493
  • 3
  • 28
  • 48
0

You could use BufferedReader.readLine() to read a large file line by line.

Andreas Hartmann
  • 1,825
  • 4
  • 23
  • 36