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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2744962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 22:38:39  来源:igfitidea点击:

Load/Store Objects in file in Java

javafileloadstore

提问by brain_damage

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

回答by Shashank T

The solution of this problem is that when you are using other objects, let say class A, into a collection like HashMapand want to serialize the HashMapobject, then implementthe interface Serializablefor class Alike this:

这个问题的解决方案是,当你使用其他对象时,比如 class A,进入一个集合,HashMap并想要序列化HashMap对象,然后类implement的接口是这样的:SerializableA

class A implements Serializable {
}

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

Otherwise that object will not be serializable.

否则该对象将无法序列化。

I hope it will solve this problem now.

我希望它现在可以解决这个问题。

回答by Cameron

Try oos.flush() before you close it.

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

回答by Daniel

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 就不会序列化它们。