user1621988
user1621988

Reputation: 4335

Save/Load HashMap with .bin

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;
}

How can I Save/Load HashMap's.
I use this methode, but it seem's to have problems.
The barrack.bin contains some error's that I can't figure out.
(java.io.NotSerializableException java.io.ObjectStreamException IOException suppressedExceptionst)

Upvotes: 0

Views: 338

Answers (1)

Reimeus
Reimeus

Reputation: 159754

Your class BarrackData does not appear to implement java.io.Serializable. It should look like this:

public class BarrackData implements Serializable {
   ...

Upvotes: 1

Related Questions