我正在尝试序列化 Path2D.Double 的子类,序列化有效,但反序列化不是因为以下异常:
Exception in thread "main" java.io.InvalidClassException: CustomShape; no valid constructor
at java.io.ObjectStreamClass$ExceptionInfo.newInvalidClassException(ObjectStreamClass.java:147)
at java.io.ObjectStreamClass.checkDeserialize(ObjectStreamClass.java:755)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1751)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
at CustomShape.main(CustomShape.java:38)
这可能是因为超类 Path2D 没有实现 Serializable 吗?但是,Path2D.Double 本身也无法反序列化。
这是我的代码:
import java.awt.geom.Path2D;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class CustomShape extends Path2D.Double {
private static final long serialVersionUID = 1L;
public CustomShape() {
super();
}
public static void main(String[] args) throws Exception {
//Make Shape
//Path2D shape = new Path2D.Double(); //works
Path2D shape = new CustomShape(); //doesn't work
shape.moveTo(0, 0);
shape.lineTo(0, 10);
shape.lineTo(10, 10);
shape.lineTo(10, 0);
shape.closePath();
File file = new File(System.getProperty("user.dir"), "shape");
//Save Shape
if (!file.exists()) {
file.createNewFile();
}
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));
out.writeObject(shape);
out.flush();
out.close();
//Load Shape
ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
Object obj = in.readObject();
in.close();
System.out.println(obj);
}
}