Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how to read text file backwards  RSS feed

 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all
is there any way to read text file from its end ?
i have some log file that i like to read from its last line and 10 lines up
can it be done in java?

thanks
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you could read the lines and store them in a java.util.List and then iterate the list backwards:

 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can read through the file and only keep the last n lines you read with a "circular buffer". Say with a 10 line array ...

See what happens to the array index when you read line the 11th line or the 101st? I'll leave it to figure out how to print the last 10 lines in order.

If you don't want to read the whole file - too many megabytes or whatever - you can do some trickier stuff with random access file and your own backwards buffered reader.

Any of that sound like it will help?
[ October 07, 2006: Message edited by: Stan James ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!