• 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

Randomaccessfile

 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello everybody,
To append the character to the end of the Randomaccess file,tell me which one is correct.
1)seek(length()-1);
2)seek(length());
I think second one is correct but I am not sure,please tell me the right one.
First one will position the pointer just before the last byte and the next read and write operation will be done on the last byte whereas the second option will move the pointer to the end of the last byte therefore next read and write operation will be done beyond the last byte.Please correct me if am wrong.
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nitin,
I think that the form which will get the pointer to the end of file is
seek(length())
length() returns the totalbytes infile.
seek() positions the pointer just before the byte specified as it argument.
so if we seek to the length() then the pointer will be positioned just after the last byte coz the pointer position starts from 0 (zero).
seek(length()-1) will place the pointer just before the last character.
I hope it is correct.
Do correct me if I am wrong.
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Nitin.
Neeraj is correct.
raf.seek( raf.length() ) starts writing after the file's last character; raf.seek( raf.length() - 1 ), on the other hand, will overwrite the file's endmost character, which you don't want.
Art
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic