File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

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?


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

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 |
------------------------------------- (i.e. 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.

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

[Blog] [Blogroll] [My Reviews] My Linked In
I agree. Here's the link:
subject: End of file
It's not a secret anymore!