“对象序列化”是什么意思?你能举例解释一下吗?


当前回答

我将提供一个类比,以潜在地帮助巩固对象序列化/反序列化的概念目的/实用性。

I imagine object serialization/deserialization in the context of attempting to move an object through a storm drain. The object is essentially "decomposed" or serialized into more modular versions of itself - in this case, a series of bytes - in order to effectively be granted passage through a medium. In a computational sense, we could view the path traveled by the bytes through the storm drain as being akin to bytes traveling through a network. We're transmuting our object in order to conform to a more desirable mode of transportation, or format. The serialized object will typically be stored in a binary file which may later be read from, written to, or both.

也许一旦我们的对象能够作为分解的字节序列通过drain,我们可能希望将对象的表示形式作为二进制数据存储在数据库或硬盘驱动器中。不过,主要的要点是,通过序列化/反序列化,我们可以选择让对象在序列化后保持二进制形式,或者通过执行反序列化来“检索”对象的原始形式。

其他回答

序列化一个类:将一个对象转换为字节和字节返回到对象(反序列化)。

class NamCls implements Serializable
{
    int NumVar;
    String NamVar;
}

对象序列化是将对象的状态转换为字节蒸汽的过程。

|->当您希望对象在JVM生命周期之外仍然存在时实现。 |->序列化对象可以存储在数据库中。 |->可序列化对象不能被人类读取和理解,因此我们可以实现安全性。

对象-反序列化是获取对象状态并将其存储到对象(java.lang.Object)的过程。

|->在存储它的状态之前,它检查serialVersionUID表单input-file/network和.class文件serialVersionUID是否相同。 如果不抛出java.io.InvalidClassException。

一个Java对象只有在它的类或它的任何超类时才是可序列化的

实现java.io.Serializable接口或 它的子接口java.io.Externalizable。

|=>类中的静态字段不支持序列化。

class NamCls implements Serializable
{
    int NumVar;
    static String NamVar = "I won't be serializable";;
}

如果你不想序列化一个类的变量,使用transient关键字

class NamCls implements Serializable
{
    int NumVar;
    transient String NamVar;
}

如果一个类实现了可序列化,那么它的所有子类也都是可序列化的。

|=>如果一个类有另一个类的引用,所有的引用必须是Serializable,否则序列化过程将不会执行。在这种情况下,NotSerializableException将在运行时抛出。

序列化是将Java对象转换为字节数组,然后再将其转换为保留状态的对象的过程。适用于通过网络发送对象或将内容缓存到磁盘等各种事情。

请阅读这篇简短的文章,它很好地解释了编程过程的一部分,然后转到Serializable javadoc。你也可能对这个相关的问题感兴趣。

序列化是将对象转换为一系列字节,以便可以轻松地将对象保存到持久存储器或通过通信链路进行流式传输。然后可以反序列化字节流,将其转换为原始对象的副本。

我将提供一个类比,以潜在地帮助巩固对象序列化/反序列化的概念目的/实用性。

I imagine object serialization/deserialization in the context of attempting to move an object through a storm drain. The object is essentially "decomposed" or serialized into more modular versions of itself - in this case, a series of bytes - in order to effectively be granted passage through a medium. In a computational sense, we could view the path traveled by the bytes through the storm drain as being akin to bytes traveling through a network. We're transmuting our object in order to conform to a more desirable mode of transportation, or format. The serialized object will typically be stored in a binary file which may later be read from, written to, or both.

也许一旦我们的对象能够作为分解的字节序列通过drain,我们可能希望将对象的表示形式作为二进制数据存储在数据库或硬盘驱动器中。不过,主要的要点是,通过序列化/反序列化,我们可以选择让对象在序列化后保持二进制形式,或者通过执行反序列化来“检索”对象的原始形式。

Serialization is the process of saving an object in a storage medium (such as a file, or a memory buffer) or to transmit it over a network connection in binary form. The serialized objects are JVM independent and can be re-serialized by any JVM. In this case the "in memory" java objects state are converted into a byte stream. This type of the file can not be understood by the user. It is a special types of object i.e. reused by the JVM (Java Virtual Machine). This process of serializing an object is also called deflating or marshalling an object.

要序列化的对象必须实现java.io.Serializable Interface。 对象的默认序列化机制写入对象的类、类签名以及所有非瞬态和非静态字段的值。

class ObjectOutputStream extends java.io.OutputStream implements ObjectOutput,

ObjectOutput接口扩展了DataOutput接口,并添加了用于序列化对象和向文件写入字节的方法。ObjectOutputStream扩展了java.io.OutputStream并实现了ObjectOutput接口。它将对象、数组和其他值序列化到流中。因此ObjectOutputStream的构造函数被写成:

ObjectOutput ObjOut = new ObjectOutputStream(new FileOutputStream(f));

上面的代码已用于使用ObjectOutputStream()构造函数创建ObjectOutput类的实例,该构造函数将FileOuputStream的实例作为参数。

ObjectOutput接口用于实现ObjectOutputStream类。构造ObjectOutputStream是为了序列化对象。

在java中反序列化对象

与序列化相反的操作称为反序列化,即从一系列字节中提取数据称为反序列化,也称为膨胀或解组。

ObjectInputStream扩展了java.io.InputStream,实现了ObjectInput接口。它反序列化来自输入流的对象、数组和其他值。因此ObjectInputStream的构造函数被写成:

ObjectInputStream obj = new ObjectInputStream(new FileInputStream(f));

上面的程序代码创建了ObjectInputStream类的实例,以反序列化已被ObjectInputStream类序列化的文件。上面的代码使用FileInputStream类的实例创建实例,FileInputStream类包含必须反序列化的指定文件对象,因为ObjectInputStream()构造函数需要输入流。