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.
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?
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.
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.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi