简体   繁体   中英

Serializing objects in java

Just did a brief reading to try and implement serialization into my code.

Basically, I have a Test class that holds a bunch of Questions . I would either like to serialize the whole Test object at once if possible, or if not each Question . Right now I am trying to do each Question .

public abstract class Question implements Serializable {
    ...
}

public class Test {

...

public void save() {
    try {

        System.out.print("File name will be saved.ser: ");

        FileOutputStream fileOut = new FileOutputStream("saved.ser");

        ObjectOutputStream out = new ObjectOutputStream(fileOut);

        for (Question question : questionList) {
            out.writeObject(question);
        }

        out.close();
        fileOut.close();

    } catch(IOException i) {
        i.printStackTrace();
    }
}

And then it gives this error:

java.io.NotSerializableException: java.util.Scanner
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at main.Survey.save(Survey.java:129)
    at main.MainDriver.main(MainDriver.java:59)

Is the problem that not every object in Question implements Serializable ? Does that need to be done as well? That seems quite tedious, but I'll do it if necessary. Do all of my classes that are used by Question , including it's subclasses, need to be given that interface?

Also, would I be better off Serializing the whole Test object in my main method?

Is the problem that not every object in Question implements Serializable? Does that need to be done as well?

Yes, all members of a class that is serializable, must also be serializable.

Do all of my classes that are used by Question, including it's subclasses, need to be given that interface?

No, if the super class is serializable, then all its sub classes are also serializable, implicitly. So, you don't need to mark the sub classes as serializable explicitly again.

In your case, I think Scanner object need not be a class member and can be a local variable where you are reading input. If you still need Scanner object to be a member variable, declare it as transient so that it is ignored during serialization and deserialization process.

java.util.Scanner does not implement the Serializable interface. You have to declare the field as transient in the class which contains the Scanner instance field.

It would be better to omit it from the class instance variables and use it locally in the method. In this way you wont have to mark it transient in the class.The authors of the Java specification are particular about which classes are meant to be serializable or not. Wrapper classes like Integer , Float,... and collection classes like ArrayList , HashMap... implement the java.io.Serializable interface. The Scanner class is not meant to be Serializable from the point of view of the authors of the java specification. From that we can decide which class to make Serializable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM