views:

165

answers:

3

I want to store an object from my class in file, and after that to be able to load the object from this file. But somewhere I am making a mistake(s) and cannot figure out where. May I receive some help?

public class GameManagerSystem implements GameManager, Serializable {

    private static final long serialVersionUID = -5966618586666474164L;
    HashMap<Game, GameStatus> games;
    HashMap<Ticket, ArrayList<Object>> baggage;
    HashSet<Ticket> bookedTickets;
    Place place;


    public GameManagerSystem(Place place) {
        super();

        this.games = new HashMap<Game, GameStatus>();
        this.baggage = new HashMap<Ticket, ArrayList<Object>>();
        this.bookedTickets = new HashSet<Ticket>();
        this.place = place;
    }
    public static GameManager createManagerSystem(Game at) {
        return new GameManagerSystem(at);
    }

    public boolean store(File f) {
        try {
            FileOutputStream fos = new FileOutputStream(f);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(games);
            oos.writeObject(bookedTickets);
            oos.writeObject(baggage);
            oos.close();
            fos.close();
        } catch (IOException ex) {
            return false;
        }
        return true;
    }
    public boolean load(File f) {
        try {
            FileInputStream fis = new FileInputStream(f);
            ObjectInputStream ois = new ObjectInputStream(fis);
            this.games = (HashMap<Game,GameStatus>)ois.readObject();
            this.bookedTickets = (HashSet<Ticket>)ois.readObject();
                this.baggage = (HashMap<Ticket,ArrayList<Object>>)ois.readObject();
            ois.close();
            fis.close();
        } catch (IOException e) {
            return false;
        } catch (ClassNotFoundException e) {
            return false;
        }
        return true;
    }
.
.
.
}


public class JUnitDemo {

    GameManager manager;

    @Before
    public void setUp() {
        manager = GameManagerSystem.createManagerSystem(Place.ENG);
    }

    @Test
    public void testStore() {
        Game g = new Game(new Date(), Teams.LIONS, Teams.SHARKS);
        manager.registerGame(g);
        File file = new File("file.ser");
        assertTrue(airport.store(file));
    }
}
A: 

Try oos.flush() before you close it.

Cameron
All OutputStreams do flush() before close()
adrian.tarau
...at least the ones well implemented and this includes what's in JRE.
adrian.tarau
+1  A: 

The solution of this problem is that when you are using other objects, let say class A, into a collection like HashMap and want to serialize the HashMap object, then implement the interface Serializable for class A like this:

class A implements Serializable {
}

...
    HashMap<Integer,A> hmap;
...

Otherwise that object will not be serializable.

I hope it will solve this problem now.

Shashank T
A: 

Please remenber that the whole object graph is persisted during serialize. If you have some references to GUI classes for example, you either have to make them serializable, too, or tag them as "transient", so Java won't serialize them.

Daniel