简体   繁体   English

使用Java在文件中加载/存储对象

[英]Load/Store Objects in file in Java

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

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: 这个问题的解决方案是当你使用其他对象时,比如说A类,进入像HashMap这样的集合,想要序列化HashMap对象,然后为类A implement Serializable接口,如下所示:

class A implements Serializable {
}

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

Otherwise that object will not be serializable. 否则该对象将不可序列化。

I hope it will solve this problem now. 我希望它现在能解决这个问题。

在关闭它之前尝试oos.flush()。

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. 例如,如果你有一些GUI类的引用,你或者必须使它们可序列化,或者将它们标记为“瞬态”,因此Java不会将它们序列化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM