• 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

Random Access File...

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all...
According to Java API:- The Seek() will read the next byte from the file pointer position. The first byte being at position 0.
Now look at the below snippet of code:-
{
RandomAccessFile raf = new RandomAccessFile("test1.txt" , "rw");
for (int i=0; i<10; i++)
raf.write(i);
raf.seek(1);
out = raf.readByte();
raf.close();
System.out.println(out);
}
// The No's written will be :- 0 1 2 3 4 5 6 7 8 9 (Each occupying 1 byte.)
So here the filepointer using seek(1) will be placed on 1 & the next
byte read will be 2, but here the output is 1.
Can someone explain me where i m wrong.
Also i want to know how many bytes does the method raf.write(i) takes
Thanks.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shah,
Well, actually, the API says


the offset position, measured in bytes from the
beginning of the file, at which to set the file pointer.


As you wrote '0', '1', '2', etc to the file; '0' occupies the first byte, '1', the second, etc.
So <code>raf.seek(1)</code> is saying 'skip 1 byte' which positions the read for the second byte or '1'.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Anderson gave himself the promotion. So I gave myself this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic