0
public Map<String, BarrackData> barrack = new HashMap<String, BarrackData>();
SavingData.save(barrack, "barrack.bin"); // save
barrack = (Map<String, BarrackData>)SavingData.load("barrack.bin"); // load
// BarrackData contains 3 int's and 1 String.

public static void save(Object obj, String path) throws Exception {
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(path));
    oos.writeObject(obj);
    oos.flush();
    oos.close();
}

public static Object load(String path) throws Exception {
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream(path));
    Object result = ois.readObject();
    ois.close();
    return result;
}

如何保存/加载 HashMap。
我使用这种方法,但它似乎有问题。
barrack.bin 包含一些我无法弄清楚的错误。
(java.io.NotSerializableException java.io.ObjectStreamException IOException suppressExceptionst)

4

1 回答 1

1

您的课程BarrackData似乎没有实现java.io.Serializable。它应该如下所示:

public class BarrackData implements Serializable {
   ...
于 2012-10-23T20:28:07.990 回答