jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes reading files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "reading files" Watch "reading files" New topic

reading files

kevin schmidt
Ranch Hand

Joined: Aug 24, 2001
Posts: 85
How does a person go about reading in a text file starting from the end instead of the beginning of it? TIA.
Martin Rennix
Ranch Hand

Joined: Sep 30, 2001
Posts: 34
At a guess, try java.io.RandomAccessFile and use seek() to go to the end of the file. You'll need to use seek() to move further back into the file after each read(). Remember seek() uses bytes so if you are reading Unicode chars using readChar() you'll need to move 4 bytes back (the 2 bytes you have just read plus another 2 to go the previous char.)
I hope that makes sense!
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

Another way is you could read the file beginning to end into an array, then just step through the array backwards while doing the work you need to do in the file.

Happy Coding,
Gregg Bolinger

GenRocket - Experts at Building Test Data
I agree. Here's the link: http://aspose.com/file-tools
subject: reading files
It's not a secret anymore!