How to read specified line from a text file
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   alex_goo
Posted On:   Tuesday, April 15, 2003 11:58 PM

I want to read lines from a txt file. But I am no desired to read them by a loop with BufferedReader.readline().
Is there anyone can tell me how to read specified line(such as the first line, the tenth line etc.) from a txt file?
Thanks

Re: How to read specified line from a text file

Posted By:   Christopher_Koenigsberg  
Posted On:   Wednesday, April 16, 2003 09:45 AM

What do you think a "line" in a "file" is? Unless you are on a mainframe OS, or VMS, where "record-oriented" files are available (hence each "line" is actually a separate physical "record"), a file is just a stream of bytes. As far as I know, you have to go through the file's bytestream and read bytes to find ones that form line terminator sequences ("
", or "
", or "
", or "
", depending on the native OS libraries used to write the file, and determined for your application by a system property) to determine where "lines" begin or end. As far as I know, any "readline" method is actually just a convenient wrapper, reading byte by byte in a loop, until it gets the next (system defined) line terminator sequence, and then stops.



Maybe you have "random access" to jump to a particular numbered byte in the file, but still as far as I know, there is nothing special about the line terminator bytes, so jumping to byte #400, without reading all the preceding bytes, tells you nothing about whether there are 0, 1, 10, 100, or 399 "lines", before that point.

About | Sitemap | Contact