【发布时间】:2014-02-10 21:41:13
【问题描述】:
我如何序列化包含不可序列化对象的哈希映射,在我的例子中是 JavaFX 组件?
final HashMap<String, Button> mapButton = new HashMap<>();
// some for loop adding the components..
try {
FileOutputStream fileOut = new FileOutputStream("Resources/");
ObjectOutputStream objStream = new ObjectOutputStream(fileOut);
objStream.writeObject(mapButton);
objStream.close();
fileOut.close();
System.out.println("Serialized HashMap mapButtons has been stored"
+ " in /tmp/store");
} catch (IOException i) {
i.printStackTrace();
}
投掷:
java.io.NotSerializableException: javafx.scene.control.Button
【问题讨论】:
-
你不能序列化不可序列化的对象。期间。
-
你看,HashMap 对象本身实际上是可序列化的,只有它包含的对象不是。因此我认为这是可能的。
-
是的,但是HashMap如何序列化它的项目?
标签: java serialization hashmap javafx components