posted 20 years ago
The only way to find out where lines begin in a file is by reading through the file from the beginning, at least once. That's because there's no way of knowing in advance which bytes will represent '\r' or '\n' - you've got to check them all. (Or, all up to whatever line number you're interested in.) Now if you're going to refer to one file repeatedly, there are things you can do to make subsequent line lookup much easier. The simplest is to use a BufferedReader to read lines with readLine(), and create a List of Strings containing all the lines read. If the file is big and you don't have enough memory, you could create some sort of index instead, e.g. showing where each line begins, or every 100 lines, something like that. This is a bit complex though - stick with the List of Strings idea if you can.
"I'm not back." - Bill Harding, Twister