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?