Get the tools you need to learn Java skills fast!
Video tutorials, eBooks, hands-on lab exercises, sample code.
Get started
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 Badass: Making Users Awesome this week in the Game Development 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: 11624

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