File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Checking EOF with raf.readShort() 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 "Checking EOF with raf.readShort()" Watch "Checking EOF with raf.readShort()" New topic

Checking EOF with raf.readShort()

Vishwa Kumba
Ranch Hand

Joined: Aug 27, 2003
Posts: 1066

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

Joined: Jun 02, 2003
Posts: 1872
Hi Vishwa,

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


I agree. Here's the link:
subject: Checking EOF with raf.readShort()
It's not a secret anymore!