File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes How to Go to a new Line using RandomAccessFile Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to Go to a new Line using RandomAccessFile" Watch "How to Go to a new Line using RandomAccessFile" New topic

How to Go to a new Line using RandomAccessFile

Hrishikesh Hemade

Joined: Mar 17, 2005
Posts: 9
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 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

Joined: Aug 10, 2005
Posts: 692
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.

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

Joined: Aug 03, 2002
Posts: 7729
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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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.


Java API Documentation
The Java Tutorial
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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
I agree. Here's the link:
subject: How to Go to a new Line using RandomAccessFile
It's not a secret anymore!