Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Checking EOF with raf.readShort()  RSS feed

 
Vishwa Kumba
Ranch Hand
Posts: 1066
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I am using RandomAccessFile(raf).

raf.readBytes(byte b[]) return -1 if end of file is encountered, but I am also using raf.readShort as shown above. readShort() does not return -1 but throws EOFException.

To check for the eof in my code, is catching EOFException the only way? I could use read() or read(byte[]) which return -1 for EOF and then convert the 2 bytes to short, but that is the long way. Is there a better way to do this?
 
Philippe Maquet
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vishwa,

Another way of doing is computing the last record position in the file (file size - record length).

Regards,

Phil.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!