title: Deserialize only if a serialized file exists
body: <p>I have my program setup to serialize a certain Facebook Object on exit, and deserialize it on open. But I want to have an if statement in the deserialization portion to only go on with it if a serilization file is present to avoid errors. How can I do that? Is there anyway that I could refer to the file "serilaized" and check if it exists?</p>
  
  <p>Deserialization portion:</p>
  
  <pre><code>Facebook facebook = null;
  
      try {
          ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
                  "serialized"));
  
          facebook = (Facebook) ois.readObject();
  
          ois.close();
      } catch (FileNotFoundException e) {
          System.err.println("Could not open the file \"serialized\"");
      } catch (IOException e) {
          System.err.println("Could not de-serialize the object");
      } catch (ClassNotFoundException e) {
          System.err.println("Could not cast the de-serialized object");
      }
  </code></pre>
  
  <p>Serialization portion:</p>
  
  <pre><code>try {
                  ObjectOutputStream oos = new ObjectOutputStream(
                          new FileOutputStream ("serialized"));
  
                  oos.writeObject(facebook);
                  oos.close();
              } catch (FileNotFoundException e) {
                  System.err
                          .println("Could not create the file \"serialized\"");
              } catch (IOException e) {
                  System.err.println("Could not serialize the object");
              }
  </code></pre>