• 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

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi,
The right answer is [c] but I think it should be [d]. Please help me to understand this problem.
Thanks,
Arun
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Arun Mishra:

Hi,
The right answer is [c] but I think it should be [d]. Please help me to understand this problem.
Thanks,
Arun


Imagine a file that is 10 bytes long. When you create a RandomAccessFile object, the file pointer points to position 0, immediately before the first byte of the file. Now, if you call length, it will return the value 10 (since length returns the length of the file in bytes). If you send that value to seek, it will move the pointer 10 bytes forward, which puts it one before the final byte, because the pointer originally started one before the first byte. The key is that the value seek moves to is measured from the beginning of the file, which is position 0.
I hope this helps.
Corey
 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I don't know the answer to your question but I have some other questions related to it. When seek() performs as you have specified does it start counting at 1 or 0? This may help us to determine where the pointer should be.
Also, where does the pointer start--before the first character or after it?
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When RandomAcessFile is created the pointer is at 0 bytes. The last byte of the files has an index of length-1. The API for seek doesn't mention that any Exception is thrown if seek is given an argument greater than length-1.
Try
System.out.println(new RandomAcccessFile("file").getFilePointer());
to be sure
[ January 10, 2002: Message edited by: Jose Botella ]
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi -
I spent some time running a test on this. Here's my code and the resulting output

So, setting seek to raf.length() moves the pointer to byte 12, one byte AFTER the last byte in the file. And, it seems that it is perfectly ok to set the pointer to any number at all - ie, setting it to raf.length() + 20, way beyond the length of the file, caused no exceptions. It's meaningless, but the JVM doesn't care.
Sylvia
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the code Sylvia
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also think answer is d.
-SR
reply
    Bookmark Topic Watch Topic
  • New Topic