错误:java.io.NotSerializableException

时间:2014-03-10 12:01:14

标签: java serialization

我有一个类定义如下

public class EvaluationResult implements java.io.Serializable {
    public String ClassName;
    public boolean FeedbackDirected;
    public double HV;
    public double Spread;
    public double PercentageOfCorrectness;
    public double TimeElapsed;
    public ArrayList<double[]> ParetoFront;


}

这样做

public static void main(String[] args) throws Exception {
         EvaluationResult ae=new EvaluationResult();
         FileOutputStream fileOut =new FileOutputStream("result.txt");
         ObjectOutputStream out = new ObjectOutputStream(fileOut);
         out.writeObject(fileOut);
         out.close();
         fileOut.close();
     }

给我java.io.NotSerializableException有什么问题?

3 个答案:

答案 0 :(得分:3)

您需要实现Serializable标记接口

 public class EvaluationResult implements Serializable { .... }

您应该编写对象(而不是文件流):

 out.writeObject(ae);

答案 1 :(得分:3)

您必须在类定义中实现Serializable

public class EvaluationResult implements Serializable {
    public String ClassName;
    public boolean FeedbackDirected;
    public double HV;
    public double Spread;
    public double PercentageOfCorrectness;
    public double TimeElapsed;
    public ArrayList<double[]> ParetoFront;

}

答案 2 :(得分:1)

您必须实现Serializable接口......这意味着实现以下内容:

在序列化和反序列化过程中需要特殊处理的类必须使用这些精确签名实现特殊方法:

 private void writeObject(java.io.ObjectOutputStream out) throws IOException
 private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException;
 private void readObjectNoData() throws ObjectStreamException;

tutorial may be of use