Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to detect the end of file  RSS feed

 
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: 12143
255
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!