aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes End of file 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 "End of file" Watch "End of file" New topic
Author

End of file

Roger Chan
Ranch Hand

Joined: Nov 13, 2001
Posts: 30
What is the correct way of specifying the end of a file when using RandomAccessFile class?

or

I found conflicting information from two sources. That is why I want to confirm.
Thanks
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
length returns the length of the file measured in bytes, i.e. if length returns 10 it means that your file is ten bytes long from byte 0 to byte 9. If you positioned (seek) the file pointer with the value returned by length, you will have your pointer just after the last byte.
The file looks like this:
-------------------------------------
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
-------------------------------------
oraf.seek(oraf.length()) (i.e. oraf.seek(10)) will result in setting the file pointer at position 10.
If you want to point to the last byte in the file then you have to use the length()-1 offset. In that case the pointer points to byte 9.
HIH

------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
[This message has been edited by Valentin Crettaz (edited December 02, 2001).]


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: End of file