Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Checking EOF with raf.readShort()

 
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic