APPEND data at a specific line within a file using RandomAccessFile
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   ynoT_Tony
Posted On:   Wednesday, July 17, 2002 11:09 AM

I am trying to use RandomAccessFile. The problem that I am having is that the methods available : getFilePointer(), and seek() dont refer to a specific line on the file, but to a Byte position. I have tried to create a counter to determine at what line in my file the pointer is at. But, I cant tell what the corresponding Byte is to it. I use getFilePointer, but thats not at the next line. My file is basically a growing/shrinking html page with tags and everytime this file gets updates, the positions are messed up, so I dont know where to append the data. I do know at what line I want to append it to, but RandomAccessFile doesnt have that feature to seek() for a line rather than a byte position. If anyone can be of any help, please fe   More>>

I am trying to use RandomAccessFile. The problem that I am having is that the methods available : getFilePointer(), and seek() dont refer to a specific line on
the file, but to a Byte position. I have tried to create a counter to determine at what line in my file the pointer is at. But, I cant tell what the corresponding Byte is
to it. I use getFilePointer, but thats not at the next line. My file is basically a growing/shrinking html page with tags and everytime this file gets updates, the
positions are messed up, so I dont know where to append the data. I do know at what line I want to append it to, but RandomAccessFile doesnt have that
feature to seek() for a line rather than a byte position. If anyone can be of any help, please feel free. Thanks... Tony.

   <<Less

Re: APPEND data at a specific line within a file using RandomAccessFile

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.

About | Sitemap | Contact