SERIALIZE HASHMAP: 此代碼工作正常,我已經實現並在我的應用程序中使用。 Plz使你的功能相應地保存地圖和檢索地圖。
重要的是,您需要確認您在映射中放置爲值的對象必須是可序列化的,意味着它們應該實現serailizbele接口。恩。 Map < .String,String> hashmap = new HashMap < .String,String>()..在這一行中... map和string都是隱式可序列化的,所以我們不需要爲這些明確地實現可串行化,但是如果你把你自己的對象必須是可序列化的。
public static void main(String arr[])
{
Map<String,String> hashmap=new HashMap<String,String>();
hashmap.put("key1","value1");
hashmap.put("key2","value2");
hashmap.put("key3","value3");
hashmap.put("key4","value4");
FileOutputStream fos;
try {
fos = new FileOutputStream("c://list.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(hashmap);
oos.close();
FileInputStream fis = new FileInputStream("c://list.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
Map<String,String> anotherList = (Map<String,String>) ois.readObject();
ois.close();
System.out.println(anotherList);
} catch (FileNotFoundException e) {e.printStackTrace();
} catch (IOException e) {e.printStackTrace();
} catch (ClassNotFoundException e) {e.printStackTrace();
}
}
嘗試刷新輸出,並關閉它。 – Sid
你應該總是關閉你的流,添加'f_out.close();'在你的'迴歸真實'之前。 – pushy
我剛剛做了,src文件夾裏沒有任何東西改變 –