File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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
Author

How to detect the end of file

david hu
Ranch Hand

Joined: Jul 20, 2001
Posts: 143
Hi,

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: 1064
http://www.coderanch.com/t/185678/java-developer-SCJD/certification/Checking-EOF-raf-readShort

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

Joined: Mar 28, 2003
Posts: 11278
    
  59

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: http://aspose.com/file-tools
 
subject: How to detect the end of file
 
Similar Threads
Checking for EOF
Detecting EOF?
Detecting the end of a text file...
java redirection
End of file JPG images