序列化之间的区别是什么,只是将对象存储在磁盘上? [英] What's the difference between Serialization and simply store the object on disk?

查看:328
本文介绍了序列化之间的区别是什么,只是将对象存储在磁盘上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此感到困惑。
因为在实现Serializable类时,我们需要使用像 FileOutputStream ObjectOutputStream 这样的类或类似的东西那。那么为什么我们不只是使用这些类来做一些事情,比如将对象输出到文件并从文件中输入对象以直接维护对象的状态?我们为什么要先实现Serializable然后做同样的事情?

I am confused about this. Since when doing implementation of Serializable class, we need to use classes like FileOutputStream, ObjectOutputStream or something like that. Then why not we just use those classes to do things like output a object to a file and input a object from a file to maintain the status of a object directly? Why should we first implement Serializable and then do the same thing?

推荐答案

这样理解......

Understand it like this...

Serializable是标记接口,表示类的对象可以转换为字节流,并在需要时最终返回到java对象。最初你可能会认为每个类都应该是可序列化的,但这是不正确的考虑

Serializable is marker interface which denotes that the object of your class can be converted into byte stream and eventually back into java object if required. Initially you might think that every class should be serializable but that's not correct consider

输入和输出流,它们有一些文件句柄可供读取。当流变得不可用时,该文件句柄被关闭。所以这个实例的序列化没有意义;和反序列化永远不会恢复该句柄。

所以这应该回答为什么需要标记为Serializable?

So this should answer why marking as Serializable is required?

现在实现定义如何写入或读取对象的方法;应该由您定义,因此您需要所有这些流对象和readObject,writeObject方法。希望这能让你对这个概念有更多的了解。

Now implementing methods which define how your object should be written or read; that should be defined by you and hence you need all those stream objects and readObject, writeObject methods. Hope this gives you a bit more understanding of this concept.

这篇关于序列化之间的区别是什么,只是将对象存储在磁盘上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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