java中的序列化和外化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1025513/
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
serialization and externalization in java
提问by
Possible Duplicate:
What is the difference between Serializable and Externalizable in Java?
what is the difference between serialization and externalization in java?
java中的序列化和外化有什么区别?
回答by coobird
Basically, the difference between Serializable
and Externalizable
is that with classes which implement Serializable
, the serialization of the object is taken care of automatically, while classes that implement Externalizable
is responsible for serializing itself, without the help of default serialization procedures.
基本上, 和 之间的区别在于Serializable
,Externalizable
对于实现 的类,Serializable
对象的序列化是自动处理的,而实现的类Externalizable
负责序列化自身,而无需默认序列化过程的帮助。
There is more information provided in the API Specification for the Externalizable
interface, and the Serializable
interface. From the Externalizable
interface documentation:
Externalizable
接口和Serializable
接口的 API 规范中提供了更多信息。从Externalizable
接口文档:
Only the identity of the class of an Externalizable instance is written in the serialization stream and it is the responsibility of the class to save and restore the contents of its instances. The writeExternal and readExternal methods of the Externalizable interface are implemented by a class to give the class complete control over the format and contents of the stream for an object and its supertypes.
只有 Externalizable 实例的类的标识被写入序列化流中,并且类负责保存和恢复其实例的内容。Externalizable 接口的 writeExternal 和 readExternal 方法由类实现,以使类完全控制对象及其超类型的流的格式和内容。
The Discover the secrets of the Java Serialization APIarticle has a discussion on the Externalizable
interface in the "Create Your Own Protocol: the Externalizable Interface" section.
该发现Java序列API的秘密文章有一个讨论Externalizable
的接口:节“创建自己的协议Externalizable接口”。
回答by William Brendel
I recommend reading an article called Understand When to Serialize v. Externalize Objects in Javathat described the differences between serialization and externalization.
我建议阅读一篇名为“了解何时序列化 v. Java中的外部化对象”的文章,该文章描述了序列化和外部化之间的区别。
First is describes what serialization is:
首先是描述什么是序列化:
The serialization of objects in Java allows you to make a byte sequence from any object that has implemented the Serializable interface; it also allows you to turn that byte sequence back into an object.
Java 中对象的序列化允许您从任何实现了 Serializable 接口的对象生成一个字节序列;它还允许您将该字节序列转换回一个对象。
Next it describes a situation in which externalization might be preferable to serialization:
接下来,它描述了一种外化可能比序列化更可取的情况:
There might be times when you have special requirements for the serialization of an object. For example, you may have some security-sensitive parts of the object, like passwords, which you do not want to keep and transfer somewhere. Or, it may be worthless to save a particular object referenced from the main object because its value will become worthless after restoring.
You can control the process of serialization by implementing the Externalizable interface instead of Serializable. This interface extends the original Serializable interface and adds writeExternal() and readExternal(). These two methods will automatically be called in your object's serialization and deserialization, allowing you to control the whole process.
有时您可能对对象的序列化有特殊要求。例如,您可能有对象的某些安全敏感部分,例如密码,您不想将其保留并转移到某处。或者,保存从主对象引用的特定对象可能毫无价值,因为它的值在恢复后将变得毫无价值。
您可以通过实现 Externalizable 接口而不是 Serializable 来控制序列化的过程。该接口扩展了原有的 Serializable 接口,并增加了 writeExternal() 和 readExternal()。这两个方法会在你的对象的序列化和反序列化过程中自动调用,让你掌控整个过程。
I recommend reading the entire article, because the excerpts above do not cover the details. The article also contains several code snippets you might find useful.
我建议阅读整篇文章,因为上面的摘录没有涵盖细节。本文还包含一些您可能会觉得有用的代码片段。