Posted By:
Christopher_Koenigsberg
Posted On:
Wednesday, July 17, 2002 11:53 AM
What is a "line"? All the file system knows (and the Java file API) is bytes. Some of the bytes happen to be the (8-bit, not 16-bit) ASCII characters which we interpret as "line terminators". So you have to count these, to know what "line" you are currently in, as you move through the file. You also have to know what the end of line convention was, when the file was written.
If your file is written using Unix end of line conventions, you'll count linefeed characters, to know when you hit the end of a line (carriage returns are not counted). If your file is written using DOS/Windows end of line conventions, you'll count carriage return + linefeed pairs, to know when you've hit end of line (just one CR or LF by itself is not enough). If your file is written using Macintosh end of line conventions, you'll count carriage returns (LF's are not counted).
If you are reading a byte stream from a network protocol, note that both SMTP and HTTP specify the use of the carriage return + linefeed pair, as an end of line marker. FTP doesn't know about end of lines, it just streams bytes, similar to the Java API.
There used to be filesystems which had file types with real "records" inside files, so each line was actually a "record" and you could randomly access records, to go to specific lines. But unless you're on a mainframe, or possibly on a VMS system with the right filetype, you've only got bytes.