title: EOFException when reading files with ObjectInputStream
body: <p>I basically have a similar problem as stated here: <a href="https://stackoverflow.com/q/2308128/1065197">EOFexception in Java when reading objectinputstream</a>, but I don't find an answer with clean code.</p>
  
  <p>The answer states that the <code>ObjectInputStream#readObject</code> will throw the exception when the reader reachs the End Of File. After looking in the web for a solution, I haven't found a solution. Could be a good and clean solution for this case?</p>
  
  <p>Note: I have tried this (but it looks ugly and is not clean code). I'm looking for a better solution:</p>
  
  <pre><code>ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
  try {
      Object o;
      while ((o = ois.readObject()) != null) {
          if (o instanceof MyClass) {
              MyClass m = (MyClass)o;
              //use the object...
          }
      }
  } catch (EOFException eofex) {
      //do nothing
  }  catch (IOException ioex) {
      throw ioex;
      //I have another try/catch block outside to control the life of the ObjectInputStream
  }
  //later in the code...
  ois.close();
  </code></pre>