File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RandomAccessFile

 
Arun Mishra
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Jason Kretzer
Ranch Hand
Posts: 280
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Jose Botella
Ranch Hand
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
sylvia weller
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the code Sylvia
 
sun ram
Ranch Hand
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also think answer is d.
-SR
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic