开发者

Load/Store Objects in file in Java

开发者 https://www.devze.com 2022-12-28 13:39 出处:网络
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 hel

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 fa开发者_如何学JAVAlse;
        }
        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));
    }
}


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.


Try oos.flush() before you close it.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号