Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

how to read text file backwards

 
Ranch Hand
Posts: 620
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic