• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to Go to a new Line using RandomAccessFile

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic