• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to detect the end of file

 
david hu
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am using randomaccessfile to get the data out of the file. How to detect the end of file?
 
Vishwa Kumba
Ranch Hand
Posts: 1066
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12056
232
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!