简体   繁体   English

如何将对象保存到文件中,以便以后可以加载?

[英]How to save an object into a file, so that i can load it later?

I've been trying to save an object, a level in my case, into a file, so that i can later load it again. 我一直试图将一个对象(在我的情况下为一个级别)保存到文件中,以便以后可以再次加载它。 Basically just a save/load function. 基本上只是一个保存/加载功能。 I can't get it to work.. I keep getting a file of 5 byte, which seems way to small for what should be in it. 我无法使它正常工作。我一直得到5字节的文件,对于其中应该包含的内容来说似乎很小。 I know that it might have something to do with Serializable, but i dont know what. 我知道这可能与Serializable有关,但我不知道是什么。 Here is my current code: 这是我当前的代码:

(btw, i hard coded the level into the program, because i dont know how to save it properly to a file yet. although that is the goal...) (顺便说一句,我将关卡硬编码到了程序中,因为我还不知道如何正确地将其保存到文件中。尽管那是目的……)

public class BufferSaveGames {

public void saveGameOutputStream(Level level) throws FileNotFoundException {

    ObjectOutputStream output;
    try {
        output = new ObjectOutputStream((new FileOutputStream("SaveGame.dat")));
        output.writeObject(level);
        output.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

and the second class Level, which it is loading: (removed code not needed for example) 第二个级别,它正在加载:(例如,不需要删除的代码)

public class Level implements MouseListener, Serializable {

private Level currentLevel;
private int aantalPoppetjesOpLevel;
private String oplossing;
private int timer;
JPanel[][] levelGrid;
Poppetje[] lijstPoppetjes;

public Level() throws IOException{

    levelGrid = new JPanel[5][5];
    lijstPoppetjes = new Poppetje[5];

    for (int a=0;a<5;a++) {
         for (int b=0;b<5;b++) {
             levelGrid[a][b] = new JPanel();
             levelGrid[a][b].setSize(100,100);
             levelGrid[a][b].setVisible(true);
             levelGrid[a][b].setLayout(null);
             levelGrid[a][b].setOpaque(false);
             levelGrid[a][b].addMouseListener(this);
         } 
    }

    //bovenste rij
    levelGrid[0][0].setLocation(10,10);
    levelGrid[0][1].setLocation(112,10);
    levelGrid[0][2].setLocation(214,10);
    levelGrid[0][3].setLocation(316,10);
    levelGrid[0][4].setLocation(418,10);




    Poppetje roodPoppetje = new Poppetje("Rood", 4, 4);

    Poppetje oranjePoppetje = new Poppetje("Oranje", 0, 4);
    Poppetje groenPoppetje = new Poppetje("Groen", 1, 2);
    Poppetje paarsPoppetje = new Poppetje("Paars", 2, 1);
    Poppetje geelPoppetje = new Poppetje("Geel", 3, 3);
    //Poppetje blauwPoppetje = new Poppetje("Blauw");

    int tempA = roodPoppetje.getLocatieX(roodPoppetje);
    int tempB = roodPoppetje.getLocatieY(roodPoppetje);
    levelGrid[tempA][tempB].add(roodPoppetje);
    lijstPoppetjes[0] = roodPoppetje;
    lijstPoppetjes[0].addMouseListener(this);

    tempA = oranjePoppetje.getLocatieX(oranjePoppetje);
    tempB = oranjePoppetje.getLocatieY(oranjePoppetje);
    levelGrid[tempA][tempB].add(oranjePoppetje);
    lijstPoppetjes[1] = oranjePoppetje;
    lijstPoppetjes[1].addMouseListener(this);

That could be done with "Serialization/Deserialization". 可以使用“序列化/反序列化”来完成。 Check out this as a first approach: 将此作为第一种方法:

http://www.wikihow.com/Serialize-an-Object-in-Java http://www.wikihow.com/Serialize-an-Object-in-Java

Also try searching with "serialziation" keyword on your favorite search engine (highly probably google hehe). 还可以尝试在您喜欢的搜索引擎(很有可能是google hehe)上使用“ serialziation”关键字进行搜索。 There are a lot of libraries that do this process in such a high level api. 有很多库在如此高级的api中执行此过程。 Also there are a lot of libraries that do this with powerful uses; 另外,还有许多库可以使用强大的功能来完成此任务。 like serialization through databases. 例如通过数据库进行序列化。 Sometimes serialization is done using JSON (as it could be a more universal way to do it). 有时,序列化是使用JSON完成的(因为这可能是更通用的方式)。 Enjoy with serialization! 享受序列化! :-) :-)

edit: also search for JSON, beautiful tool 编辑:还搜索JSON,漂亮的工具

forgot to put the following line in the OutPutStream code: 忘记在OutPutStream代码中添加以下行:

            Level levelX = new Level();

this solved the problem for the very small save game that it created. 这解决了它创建的非常小的保存游戏的问题。

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

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