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


当前回答

您可以将序列化看作是将对象实例转换为字节序列的过程(根据实现的不同,字节序列可能是二进制的,也可能不是)。

当您希望通过网络传输一个对象数据时(例如从一个JVM传输到另一个JVM),它非常有用。

在Java中,序列化机制内置于平台中,但您需要实现Serializable接口以使对象可序列化。

还可以通过将属性标记为transient来防止对象中的某些数据被序列化。

最后,您可以覆盖默认机制,并提供您自己的;这可能适用于某些特殊情况。为此,您需要使用java中的一个隐藏特性。

重要的是要注意,被序列化的是对象的“值”或内容,而不是类定义。因此方法不是序列化的。

下面是一个非常基本的示例,带有注释,以方便阅读:

import java.io.*;
import java.util.*;

// This class implements "Serializable" to let the system know
// it's ok to do it. You as programmer are aware of that.
public class SerializationSample implements Serializable {

    // These attributes conform the "value" of the object.

    // These two will be serialized;
    private String aString = "The value of that string";
    private int    someInteger = 0;

    // But this won't since it is marked as transient.
    private transient List<File> unInterestingLongLongList;

    // Main method to test.
    public static void main( String [] args ) throws IOException  { 

        // Create a sample object, that contains the default values.
        SerializationSample instance = new SerializationSample();

        // The "ObjectOutputStream" class has the default 
        // definition to serialize an object.
        ObjectOutputStream oos = new ObjectOutputStream( 
                               // By using "FileOutputStream" we will 
                               // Write it to a File in the file system
                               // It could have been a Socket to another 
                               // machine, a database, an in memory array, etc.
                               new FileOutputStream(new File("o.ser")));

        // do the magic  
        oos.writeObject( instance );
        // close the writing.
        oos.close();
    }
}

当我们运行这个程序时,文件“o.ser”被创建,我们可以看到后面发生了什么。

如果我们将:someInteger的值更改为,例如Integer。MAX_VALUE,我们可以比较输出,看看有什么不同。

下面的截图正好显示了这种差异:

你能看出区别吗?;)

在Java序列化中还有一个额外的相关字段:serialversionUID,但我想这已经太长了,无法涵盖它。

其他回答

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

序列化是将一个对象的状态转换为比特的过程,这样它就可以存储在硬盘上。当您反序列化同一对象时,它将在以后保留其状态。它允许您重新创建对象,而无需手动保存对象的属性。

http://en.wikipedia.org/wiki/Serialization

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

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中持久化对象。如果您希望保存对象的状态,并希望稍后重新构建该状态(可能在另一个JVM中),则可以使用序列化。

注意,对象的属性只会被保存。如果你想再次恢复对象,你应该有类文件,因为只存储成员变量而不存储成员函数。

eg:

ObjectInputStream oos = new ObjectInputStream(                                 
                                 new FileInputStream(  new File("o.ser")) ) ;
SerializationSample SS = (SearializationSample) oos.readObject();

serializable是一个标记接口,用来标记你的类是可序列化的。标记接口意味着它只是一个空接口,使用该接口将通知JVM该类可以被序列化。

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()构造函数需要输入流。