0

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?

Deserialization portion:

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");
    }

Serialization portion:

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");
            }
shishi
  • 63
  • 1
  • 11
  • possible duplicate of [How do I check if a file exists in Java?](http://stackoverflow.com/questions/1816673/how-do-i-check-if-a-file-exists-in-java) – fabian Sep 20 '15 at 22:21

1 Answers1

1

The FileNotFoundException should catch the case where there is no file present to deserialize. Throwing the error is fine, so long as it is handled gracefully.

Beryllium
  • 556
  • 1
  • 7
  • 20