简体   繁体   English

如何在文件中写入非序列化对象(例如Shapes),以便以后可以读取它们

[英]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. 我想做的是使用ObjectOutputStreamList<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! 谢谢!

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.

相关问题 如何将java序列化对象写入和读取到文件中 - How to write and read java serialized objects into a file 如何使用kryoSerialzation将未序列化的对象存储到rediscache中 - How to Store non-serialized object into rediscache using kryoSerialzation 向文件写入/读取一系列不同的序列化对象 - Write/Read a series of different serialized objects to/from a file 用Java编写和读取Vector到序列化文件? - Write and Read a Vector to a serialized file in Java? 方法同步,但由于非序列化的线程行为,代码产生随机结果 - Method synchronized , but code produces random result due to non-serialized thread behaviour 如何在非阻塞模式下通过SocketChannel读取和写入序列化对象? - How to read and write serialized object through SocketChannel in non-blocking mode? 如何将对象保存到文件中,以便以后可以加载? - How to save an object into a file, so that i can load it later? 如何使字段成为瞬态,以便在使用编组时不对Java对象进行序列化? - How can I make a field transient, so Java objects don't get serialized when using marshal? 如何将 TXT 文件写入“/storage/emulated/0/myapp/myfile.txt”,以便稍后从文件浏览器打开它们? - How can I write TXT files to "/storage/emulated/0/myapp/myfile.txt" so I can open them from file browser later? 在Java中,如何将对象写入文件,然后再从文件中读取该对象并将其转换回HDFS中的原始对象? - In java, how to write an object to a file and later on read it from the file and convert it back to the original object in the HDFS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM