title: read a binary file until the end
body: <p>I have a series of different object serialized into a binary file.How can I read the file until the end?</p>
  
  <pre><code>    try {
          ObjectInputStream reader = new ObjectInputStream(new FileInputStream(fname));
  
          Object obj = reader.readObject(); 
  
          if (obj instanceof Azienda) {
              Azienda a = (Azienda) obj;
              company.put(a.getCod(), a);
          } else if (obj instanceof Privato) {
              Privato p = (Privato) obj;
              privato.put(p.getCod(), p);
          }
      } catch (ClassNotFoundException cnfe) {
          cnfe.printStackTrace();
      } catch (FileNotFoundException ffe) {
          System.err.println("Error: the file was not found!");
      } catch (IOException ioe) {
          ioe.printStackTrace();
      }
  </code></pre>
  
  <p>in this way I read only once object per read. </p>
  
  <p>when I read a text file I use <code>null</code></p>