如何将类对象写入bin文件

发布于 2024-11-17 23:10:18 字数 888 浏览 3 评论 0原文

我正在使用 Java 来绘制一些巨大的图表。我有一个包含此映射的个性化类对象,它由多个哈希图组成,并调用其他也由各种对象组成的个性化类。

我的问题是,每次我运行它进行测试时,大约需要 5 分钟来映射所有内容,然后对于每个节点,可能需要 1 到 10 分钟来进行一些计算。每次我必须修复代码上的某些内容时,我都必须经历那个等待时间。

所以我的问题

是否有任何方法可以将主对象类(由几个原始和非原始对象组成)存储为二进制文件,我可以保存一次并从那时起读取?

解析将如何发生?

我已经开始研究 FileOutputStream/ObjectOutputStream 和 FileInputStream/ObjectInputStream,但我不确定我是否正确理解它们是否适合这种情况。

任何意见和建议将不胜感激。

我附上了我尝试用来保存类对象的代码。

FileOutputStream fos = new FileOutputStream("tbox.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(tboxObject);
oos.flush();
oos.close();

这是第一次运行,之后我将代码交换为:

FileInputStream fis = new FileInputStream("tbox.dat");
ObjectInputStream ois = new ObjectInputStream(fis);
tboxObject savedTboxObject = (tboxObject) ois.readObject();

我将补充说,这些对象不仅由整数和字符串组成,还由哈希图、列表和其他复杂对象的数组组成。

I am using Java to map some huge graph. I have a personalized class object that has this map, it is formed of several hashmaps and calls other personalized classes which are also made out of various objects.

My problem is that every time I run this to make tests, it takes around 5 minutes to map everything and then for each node it can take from 1 to 10 minutes to do some calculations. And every time I have to fix something on the code, I have to go through that waiting time.

So my question(s)

Is there is any way to store the main object class (which is composed of several primitive and non-primitive objects) as a binary file that I can save once and the read from then on?

How would the parsing happen?

I've started looking into the FileOutputStream/ObjectOutputStream and FileInputStream/ObjectInputStream, but I am not sure I understood correctly if they are good options for this situation.

Any comments and advice will be most appreciated.

I attach the code that I tried to use to save the class object.

FileOutputStream fos = new FileOutputStream("tbox.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(tboxObject);
oos.flush();
oos.close();

That's for the first run, after that I swapped the code for:

FileInputStream fis = new FileInputStream("tbox.dat");
ObjectInputStream ois = new ObjectInputStream(fis);
tboxObject savedTboxObject = (tboxObject) ois.readObject();

I will add that these are objects made out not only of ints and string, but of hashmaps, lists, and arrays of other also complex objects.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

仙气飘飘 2024-11-24 23:10:18

java 文档

    FileOutputStream fos = new FileOutputStream("t.tmp");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(yourObject);
    oos.close();

:确保图中的所有对象都实现了可序列化。

From the java docs:

    FileOutputStream fos = new FileOutputStream("t.tmp");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(yourObject);
    oos.close();

Just make sure all objects in the graph implement serializable.

北陌 2024-11-24 23:10:18

是的,您可以使用 OutputStream/InputStream 类序列化并保存对象。但是,当您修改类(例如添加/删除/修改实例变量)然后尝试读取以前保存的文件时,请务必小心。

Yes, you can serialize and save the objects using the OutputStream/InputStream classes. However do watch out when you modify the class (add/delete/modify an instance variable for example) and then try to read a previously saved file.

被你宠の有点坏 2024-11-24 23:10:18

Serialize 可能就是您想要的。在类中实现可串行接口,然后使用 ObjectOutputStream 将对象写入文件。

Serialize is probably what you want. Implement the serialiable interface in your class then use ObjectOutputStream to write the object to file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文