保存到二进制/序列化java [英] Saving to binary/serialization java

查看:170
本文介绍了保存到二进制/序列化java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须引用引用,


1.将帐户保存到二进制(序列化)文件。
2.从二进制(序列化)文件中加载(重新创建)帐户。

1.Save accounts to a binary (serialized) file. 2.Load (recreate) accounts from a binary (serialized) file.

首先,我正在查找示例这究竟是什么,我迷失了,在同样的场景中人们提到xml,在我的脑海中,我认为这意味着像01010011000(二进制),当我看到其他代码时,它看起来像普通的文本文件保存。

So firstly, I was looking up examples of what exactly that is and I am lost, in same scenarios people mention xml, in my head I think it means like 01010011000 (binary), and when I look at other code it looks like a normal text file save.

他究竟是什么意思,有人可以发布一个例子,或者给我一个更好地阐明这一点的网站?
一旦我看到我真正需要做的事情,我就可以轻松实现它,我只是对确切保存的内容(数据方式)和方式感到困惑。

What exactly does he mean, and can someone post an example, or give me a site that better clarifies this? Once I see what I actually need to do, I can implement it easily, I'm just confused on what exactly is being saved (data-wise) and how.

*我已经有一个选项可以通过文本文件(.txt)保存,如果我可以使用这个二进制部分的代码。

*I already have an option to save via textfile (.txt) if I can just use some of that code for this binary part.

谢谢!

这就是我现在所拥有的,我认为它仍然不起作用。

Here is what I have now, it's still not working I think.

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;

public class SerializationMain implements Serializable {

    public static void saveSerialized(Object YourObject, String filePath) throws IOException {
        ObjectOutputStream outputStream = null;
        try {
            outputStream = new ObjectOutputStream(new FileOutputStream(filePath + ".dat"));
            outputStream.writeObject(YourObject);
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (outputStream != null) {
                    outputStream.flush();
                    outputStream.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    public static Object loadSerialized(String filePath, Object[][] data1) throws IOException {
        try {
            FileInputStream fileIn = new FileInputStream(filePath);
            ObjectInputStream in = new ObjectInputStream(fileIn);
            try {
                data1 = (Object[][]) in.readObject();
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(SerializationMain.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        System.out.println(data1.length);
        return data1;
    }
}


推荐答案

假设你有一个名为帐户的课程,你只需要在课程标题的顶部实现序列化。

Assuming you have a class called "account" you simply need to implements Serializable at the top of your class header.

根据我的理解,那将是将所有数据序列化为二进制形式。当然,您仍然需要执行使用ObjectOutputStream / ObjectInputStream将类对象实际写入/读取到文件的步骤。

From my understanding, that will serialize all the data into a binary form. You will need to of course still perform the steps to actually write/read the class object out to a file using ObjectOutputStream/ObjectInputStream.

所以例如...

public class account implements Serializable
{ ...
}

然后在您的主函数中,例如,您要保存对象的位置,您将创建一个文件,将其附加到ObjectOutputStream并写出您的二进制形式的对象。

Then in your main function for example, where you want to save the object, you would create a File, attach it to an ObjectOutputStream and write out your object in binary form.

这篇关于保存到二进制/序列化java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆