File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes How to detect the end of file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "How to detect the end of file" Watch "How to detect the end of file" New topic

How to detect the end of file

david hu
Ranch Hand

Joined: Jul 20, 2001
Posts: 143

I am using randomaccessfile to get the data out of the file. How to detect the end of file?
Vishwa Kumba
Ranch Hand

Joined: Aug 27, 2003
Posts: 1066

I think the API calls either return -1 or throw an EOFException.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11765

Hi David,

An alternative (which I think is mentioned in that thread) is that you can calculate where the file should end (header length + (number of records in the file * record length)) and stop reading at that point.

Or reverse the logic - ((size of file - header length) / record length) = number of records to read.

Regards, Andrew

The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
I agree. Here's the link:
subject: How to detect the end of file
It's not a secret anymore!