根據工程師Dollery的回答,固定在底部。Java中通用類型序列化的HashMap的HashMap
目標是序列化網絡類的網絡字段。對不起,命名。
我有這個,編譯,並正確序列化。
public class Network {
private HashMap<String, HashMap<String, Number>> network;
...
public void load(String networkFile) throws Exception{
network = (HashMap<String, HashMap<String, Number>>)Utility.deserialize(Utility.load(networkFile));
}
public void save(String networkFile) throws Exception{
Utility.save(networkFile, Utility.serialize(network));
}
}
class Number implements Serializable {
...
}
我改成了這個通用的版本,編譯,但不再序列化。我需要指定T和Number的HashMap是可序列化的。
public class Network<T extends Serializable> {
private HashMap<T, HashMap<T, Number>> network;
...
public void load(String networkFile) throws Exception{
network = (HashMap<T, HashMap<T, Number>>)Utility.deserialize(Utility.load(networkFile));
}
public void save(String networkFile) throws Exception{
Utility.save(networkFile, Utility.serialize(network));
}
}
class Number implements Serializable {
...
}
這是序列化
package Bullib;
import java.io.*;
import java.util.*;
import java.util.regex.*;
public class Utility{
public static Pattern phrasePattern = Pattern.compile("[^;:!.?]+");
public static Pattern wordPattern = Pattern.compile("['A-Za-z]+");
public static Object deserialize(byte[] serialized) throws Exception {
ByteArrayInputStream bi = new ByteArrayInputStream(serialized);
ObjectInputStream si = new ObjectInputStream(bi);
return si.readObject();
}
public static byte[] serialize(Object target) throws Exception {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream so = new ObjectOutputStream(bo);
so.writeObject(target);
so.flush();
return bo.toByteArray();
}
public static byte[] load(String filename) throws Exception {
RandomAccessFile file = new RandomAccessFile(new File(filename), "r");
byte[] b = new byte[(int)file.length()];
file.read(b);
file.close();
return b;
}
public static void save(String filename, byte[] value) throws Exception {
RandomAccessFile file = new RandomAccessFile(new File(filename), "rw");
file.write(value);
file.close();
}
public static Collection<String> executeRegex(Pattern pattern, String text){
LinkedList<String> matches = new LinkedList<String>();
Matcher m = pattern.matcher(text);
while (m.find()) {
matches.add(m.group(0));
}
return matches;
}
}
這個代碼是我回來試圖與T
序列化的或者String
或Double
Exception in thread "main" java.io.NotSerializableException: Bullib.Network.Netw
ork
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at java.util.HashMap.writeObject(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at java.util.HashMap.writeObject(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at Bullib.Utility.serialize(Utility.java:21)
at Bullib.Network.Network.save(Network.java:88)
at Engine.save(Engine.java:63)
at Engine.main(Engine.java:88)
每到請求,當誤差示例測試和錯誤。正如你所看到的,它會產生與上述相同的異常。我更新了測試,以顯示它在沒有任何內容添加到地圖的情況下通過,否則失敗。
public static void main(String[] args) throws Exception{
Network<String> stringtest = new Network<String>("","","");
stringtest.save("testempty");
stringtest.load("testempty");
System.out.println("passed empty");
stringtest.placeAndMove("fill",0.0);
stringtest.save("testfilled");
stringtest.load("testfilled");
System.out.println("passed filled");
}
java: Bullib/Network/Network
passed empty
Exception in thread "main" java.io.NotSerializableException: Bullib.Network.Netw
ork
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at java.util.HashMap.writeObject(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at java.util.HashMap.writeObject(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at Bullib.Utility.serialize(Utility.java:21)
at Bullib.Network.Network.save(Network.java:88)
at Bullib.Network.Network.main(Network.java:108)
的解決辦法是改變
public class Network<T extends Serializable> {
private HashMap<T, HashMap<T, Number>> network;
...
public void load(String networkFile) throws Exception{
network = (HashMap<T, HashMap<T, Number>>)Utility.deserialize(Utility.load(networkFile));
}
public void save(String networkFile) throws Exception{
Utility.save(networkFile, Utility.serialize(network));
}
}
class Number implements Serializable {
...
}
到
public class Network<T extends Serializable> implements Serializable {
private HashMap<T, HashMap<T, Number>> network;
...
public void load(String networkFile) throws Exception{
network = (HashMap<T, HashMap<T, Number>>)Utility.deserialize(Utility.load(networkFile));
}
public void save(String networkFile) throws Exception{
Utility.save(networkFile, Utility.serialize(network));
}
}
class Number implements Serializable {
...
}
你切出錯誤的最重要的行... –
Network類本身是可串行化的嗎?另外,您沒有複製堆棧跟蹤的最上面一行。 –
道歉,讓我澄清所有這些東西 – user1982116