Forums Register Login

RandomAccessFile

+Pie Number of slices to send: Send

Hi,
The right answer is [c] but I think it should be [d]. Please help me to understand this problem.
Thanks,
Arun
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Thanks for the code Sylvia
+Pie Number of slices to send: Send
I also think answer is d.
-SR
Die Fledermaus does not fear such a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1149 times.
Similar Threads
GC doubt
RandomAccessFile
random access file
Questions from mock Exam !!!
Help on file pointer after seek()
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 06:43:15.