Java 序列化

原文地址:http://www.programcreek.com/2014/01/java-serialization/

什么是序列化?

在 Java 中,对象序列化就是将对象表示为字节序列。 字节序列包含了对象的数据和信息,然后我们就可以将序列化的对象写入文件/数据库,并从文件/数据库读取并反序列化。 我们也可以通过这些字节序列在内存中重新创建对象。

为什么需要序列化?

当你需要通过网络发送对象或者将对象存储在文件中时,通常使用序列化。 网络基础设施和硬盘只能理解比特和字节,而不能理解 Java 对象。 序列化可以将 Java 对象转换为字节,并通过网络发送或保存。

为什么要存储或传输对象? 在我的编程经验中,有以下原因促使我使用可序列化的对象:

  1. 对象创建取决于很多上下文。 一旦创建,其方法及其字段可能是其他组件所必需要的。
  2. 当一个对象被创建并且它包含很多字段时,我们不确定该使用什么。 因此将其存储到数据库中以供后面的数据分析使用。

Java 序列化的例子

以下示例展示了如何使类可序列化并将其序列化反序列化

package serialization;

import java.io.Serializable;

public class Dog implements Serializable {
    private static final long serialVersionUID = -5742822984616863149L;

    private String name;
    private String color;
    private transient int weight;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public void introduce() {
        System.out.println("I have a " + color + " " + name + ".");
    }
}
package serialization;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SerializeDemo {
    public static void main(String[] args) {
        //create an object
        Dog e = new Dog();
        e.setName("bulldog");
        e.setColor("white");
        e.setWeight(5);

        //serialize
        try {
            FileOutputStream fileOut = new FileOutputStream("./dog.ser");
            ObjectOutputStream out = new ObjectOutputStream(fileOut);
            out.writeObject(e);
            out.close();
            fileOut.close();
            System.out.printf("Serialized dog is saved in ./dog.ser");
        } catch (IOException i) {
            i.printStackTrace();
        }

        e = null;

        //Deserialize
        try {
            FileInputStream fileIn = new FileInputStream("./dog.ser");
            ObjectInputStream in = new ObjectInputStream(fileIn);
            e = (Dog) in.readObject();
            in.close();
            fileIn.close();
        } catch (IOException i) {
            i.printStackTrace();
            return;
        } catch (ClassNotFoundException c) {
            System.out.println("Dog class not found");
            c.printStackTrace();
            return;
        }

        System.out.println("\nDeserialized Dog ...");
        System.out.println("Name: " + e.getName());
        System.out.println("Color: " + e.getColor());
        System.out.println("Weight: " + e.getWeight());

        e.introduce();

    }
}

输出结果:

Serialized dog is saved in ./dog.ser
Deserialized Dog...
Name: bulldog
Color: white
Weight: 0
I have a white bulldog.

results matching ""

    No results matching ""