[英]How to write non-serialized objects, such as Shapes, in a file so they can then be read later
I have a program with a class similar to the basic example below: 我有一个类类似于以下基本示例的程序:
public class Vertex extends Circle implements Serializable {
private int vertexID;
private final static double SIZE = 10.0;
// Constructor
public Vertex(int ID, double x, double y) {
super(x, y, SIZE, Color.BLUE);
this.vertexID = ID;
}
}
What I'd like to be able to do is write a List<Vertex> myVertices
to a file using the ObjectOutputStream
, which requires that each class implements the Serializable
interface. 我想做的是使用
ObjectOutputStream
将List<Vertex> myVertices
写入文件,这要求每个类都实现Serializable
接口。 An example is shown below: 一个例子如下所示:
FileChooser fc = new FileChooser();
File fileChosen = fc.showOpenDialog(window);
try {
FileOutputStream fOutStream = new FileOutputStream(fileChosen);
ObjectOutputStream oOutStream = new ObjectOutputStream(fOutStream);
oOutStream.writeObject(myVertices);
oOutStream.close();
} catch {
// Exception handling here
}
The problem with the implementation above is that, although Vertex
implements Serializable
, the super class Circle
and its super class Shape
do not implement Serializable
. 上述实现的问题在于,尽管
Vertex
实现了Serializable
,但超类Circle
及其超类Shape
却没有实现Serializable
。 The result is that the file contains the Vertex
objects but all Shape
details are lost and default to 0. 结果是该文件包含
Vertex
对象,但是所有Shape
详细信息都丢失了,默认为0。
Is there a simple solution to this sort of problem? 有没有解决此类问题的简单方法? My only current option seems to be to create my own
Shape
/ Circle
that stores the location/display data as doubles so that it can then be Serializable
. 我当前唯一的选择似乎是创建自己的
Shape
/ Circle
,将位置/显示数据存储为双精度,以便随后可以Serializable
。 Obviously for one class this isn't too much effort, but I have a few other Shape
objects I'd like to save as well. 显然,对于一堂课来说,这并不需要付出太多的努力,但是我也想保存一些其他
Shape
对象。 I would, I assume, then have to either construct actual Shape
objects to display them. 我认为,然后必须构造实际的
Shape
对象才能显示它们。
Thanks! 谢谢!
You need to create a writeObject/readObject method which saves the additional state that you need. 您需要创建一个writeObject / readObject方法来保存所需的其他状态。
Serialization - readObject writeObject overrides 序列化-readObject writeObject覆盖
Uses of readObject/writeObject in Serialization 序列化中readObject / writeObject的使用
Why are readObject and writeObject private, and why would I write transient variables explicitly? 为什么readObject和writeObject是私有的,为什么我要显式地写瞬态变量?
Java Custom Serialization Java自定义序列化
Java: efficiency of writeObject vs writeExternal Java:writeObject与writeExternal的效率
In case somebody wants to see the actual code for a Shape
, specifically Circle
object, here is what I implemented thanks to the accepted answer. 如果有人想查看
Shape
的实际代码,特别是Circle
对象,这是我通过接受的答案实现的。 Also included an example of a List<Vertex> myVertices = new ArrayList()
: 还包括
List<Vertex> myVertices = new ArrayList()
的示例:
private void writeObject(ObjectOutputStream oos) throws IOException {
oos.writeInt(vertexID);
oos.writeObject(myVertices);
oos.writeDouble(super.getCenterX());
oos.writeDouble(super.getCenterY());
}
private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
// Read in same order as write
this.vertexID = ois.readInt();
this.myVertices = (List<Vertex>) ois.readObject();
super.setCenterX(ois.readDouble());
super.setCenterY(ois.readDouble());
// Default Circle properties
super.setRadius(SIZE); // Default
super.setFill(Color.BLUE); // Default
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.