• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to Go to a new Line using RandomAccessFile

 
Hrishikesh Hemade
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Need help,
i have opened a file using RandomAccessFile in readmode and i have saved file length when file was first read, now the file is updated i.e. new lines are added to file which are written line by line . Now i want to go to updated data i.e. to new lines added to file without reading the file from first but to start with last offset. I have tried using file.seek(prevoius stored file length) but it skips few character data starting of the newlines added into the file. So i need help to go to newline after the last file length.
 
Norm Radder
Ranch Hand
Posts: 744
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure exactly what you are asking. Where are the extra bytes?
Is your output routine adding extra characters that you don't expect?
If the file is say 100 bytes long and you add some more bytes to it, and then if you start reading at byte 100, you should get the new bytes that were added.

Example:
Original file: ABCD (4 bytes)
After adding 2 bytes: ABCDef
Now if you start reading at byte 4, you should get ef.

Could you demonstrate what is the problem you are having?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Norm.
The following code works as expected, printing 16, true, true:

So I can only suggest that you are doing some incorrect offset arithmetic somehow.
[ September 25, 2005: Message edited by: Barry Gaunt ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know the length of a "line" in advance? If not, then RandomAccessFile will not help. If so, I suspect that the above replies are correct that you are making a mistake in your calculations. Please post the code and a description of the problems you have encountered. We will be glad to help you as much as we can.

Layne
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wonder if we wrote "abcd" to a file we'd find it's length is 4, then used a printwriter to write a new LINE as described in the OP "efgh" we'd find that the printwriter put a CR/LF before thew new line. Then we'd find the new line at the 6th or 7th character, not the 5th and the length is 9 or 10, not 8.

Hrishikesh, does that describe your problem? Can you experiment to see exactly what characters are in your file before and after appending the latest line? In Windows you can use "debug name.ext" and use the "d" command to dump bytes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic