如何序列化包含不可序列化對象的hashmap,在我的情況下是JavaFX組件?使用不可序列化對象序列化hashmap
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
您不能序列化不可序列化的對象。期。 – Pietu1998
你看,HashMap對象本身實際上是可序列化的,只有它包含的對象不是。所以我認爲這可能是可能的。 – Taerus
是的,但是HashMap如何序列化它的項目呢? – Pietu1998