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
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?
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 ]
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