2013-12-20 79 views
2

根據工程師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序列化的或者StringDouble

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 { 
    ... 
} 
+1

你切出錯誤的最重要的行... –

+1

Network類本身是可串行化的嗎?另外,您沒有複製堆棧跟蹤的最上面一行。 –

+0

道歉,讓我澄清所有這些東西 – user1982116

回答

3

試試這個:

public class Network implements Serializable 
+0

感謝您指出錯字 - 這就是我在編輯框中編碼所得到的結果。網絡字段已經完全可序列化,錯誤消息是指網絡類,而不是網絡字段。我假設Bullib.Network.Network是package.package.class。如果你刪除了你給出這個答案的-1,那將會很好。謝謝。 –

+0

@Aman如果你真的想要的話,你可以重載readObject和writeObject來只使用一個字段,而不是整個類。或者,您可以將所有其他字段標記爲傳遞。 –

+0

你的意思是'暫時的'。 iPad再次襲擊? – EJP