• Post Reply Bookmark Topic Watch Topic
  • New Topic

Read A File From Bottom Up  RSS feed

 
James Ellis
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I read a file, line by line, starting from the bottom and working my way up?

Thanks!
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your lines are all exactly the same length you can do it pretty easily with RandomAccessFile. I'd bet that's not the case, tho.

You could invent your own BackwardsBufferedReader using RandomAccessFile to load one "buffer" at a time from the end and return the contents from the last \n to the end, etc. In fact that would be pretty amusing. Let me know if you can't picture the technique pretty quickly.
 
James Ellis
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Too late...my boss needed this ASAP so I just hand replaced the stuff I needed to. Thanks for the advice though.
 
payal agrawal
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the URL below:

How to read file upside/down
http://www.java-tips.org/content/view/1396/2/
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That loads the whole file into memory ... twice! I assumed the need to read backwards rather than read the whole thing and process backwards was that it was too big to fit into memory even once. The reverse buffered reader would still be fun.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!