In Java, howd do I iterate through lines in a textfile from back to front
Posted
by rogue780
on Stack Overflow
See other posts from Stack Overflow
or by rogue780
Published on 2010-04-03T15:58:00Z
Indexed on
2010/04/03
16:03 UTC
Read the original article
Hit count: 365
Basically I need to take a text file such as :
Fred
Bernie
Henry
and be able to read them from the file in the order of
Henry
Bernie
Fred
The actual file I'm reading from is >30MB and it would be a less than perfect solution to read the whole file, split it into an array, reverse the array and then go from there. It takes way too long. My specific goal is to find the first occurrence of a string (in this case it's "InitGame") and then return the position beginning of the beginning of that line.
I did something like this in python before. My method was to seek to the end of the file - 1024, then read lines until I get to the end, then seek another 1024 from my previous starting point and, by using tell(), I would stop when I got to the previous starting point. So I would read those blocks backwards from the end of the file until I found the text I was looking for.
So far, I'm having a heck of a time doing this in Java. Any help would be greatly appreciated and if you live near Baltimore it may even end up with you getting some fresh baked cookies.
Thanks!
© Stack Overflow or respective owner