wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes RandomAccessFile Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "RandomAccessFile" Watch "RandomAccessFile" New topic
Author

RandomAccessFile

Arun Mishra
Greenhorn

Joined: Jan 02, 2002
Posts: 16

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

Joined: Dec 20, 2001
Posts: 3271
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


SCJP Tipline, etc.
Jason Kretzer
Ranch Hand

Joined: May 31, 2001
Posts: 280
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?


Jason R. Kretzer<br />Software Engineer<br />System Administrator<br /><a href="http://alia.iwarp.com" target="_blank" rel="nofollow">http://alia.iwarp.com</a>
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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 ]

SCJP2. Please Indent your code using UBB Code
sylvia weller
Greenhorn

Joined: Jan 07, 2002
Posts: 29
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

Joined: Jul 03, 2001
Posts: 2120
Thanks for the code Sylvia
sun ram
Ranch Hand

Joined: Dec 18, 2001
Posts: 61
I also think answer is d.
-SR


SCSecA,SCNA,SCSA,SCWCD,SCJP
 
wood burning stoves
 
subject: RandomAccessFile