3

这就是我如何反序列化包含标识对象的 arrayList

public void deserializeArrayList(){
    String path = "./qbank/IdentificationHARD.quiz";
    try{
          FileInputStream fileIn = new FileInputStream(path);
            ObjectInputStream in = new ObjectInputStream(fileIn);
            ArrayList<Identification> list = (ArrayList<Identification>) in.readObject();
            System.out.println(list);
    }catch(Exception e){
        e.printStackTrace();
    }
}

这就是我序列化它的方式

public void saveItemIdentification(ArrayList<Identification> identification,File file){
    try{
        ObjectOutputStream out = new ObjectOutputStream(
                                      new FileOutputStream(file));
        out.writeObject(identification);
    }catch(Exception e){
        e.printStackTrace();
    }
}

但是当我反序列化它时,它给了我这个错误

java.io.InvalidClassException: quizmaker.management.Identification; quizmaker.management.Identification; no valid constructor
    at java.io.ObjectStreamClass.checkDeserialize(Unknown Source)
    at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    at java.util.ArrayList.readObject(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at java.io.ObjectStreamClass.invokeReadObject(Unknown Source)
    at java.io.ObjectInputStream.readSerialData(Unknown Source)
    at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    at quizmaker.management.Manage.deserializeArrayList(Manage.java:92)

这是第 92 行

ArrayList<Identification> list = (ArrayList<Identification>) in.readObject();

为什么会这样?

这是Identification对象的代码。

 package quizmaker.management; 
 import java.io.Serializable;
 import quizmaker.Accounts.Rights.IAnswerable;

public class Identification extends Question implements Serializable{

    private static final long serialVersionUID = 2L;
    private String question;
    private String answer;

    public Identification(String q , String a){
        super(q,a);
    }

    public String toString(){
        return String.format("Question: %s\n Answer %s", getQuestion(),getAnswer());
    }
}
4

1 回答 1

13

问题是->在java中

Java serialization process  only continues in object hierarchy till the class
is Serializable i.e. implements Serializable interface in Java.

在您的类中,您正在调用super未实现可序列化的类构造函数。

所以这就是问题所在.. :)

对于您的第二个问题,请查看JavaDoc

During deserialization, the fields of non-serializable classes will be 
initialized using the public or protected no-arg constructor of the class.
A no-arg constructor must be accessible to the subclass that is serializable.
The fields of serializable subclasses will be restored from the stream.
于 2012-08-22T06:45:13.993 回答