序列化

将对象转换为字节流保存起来,并在以后还原这个对象,这种机制叫做序列化

 

将一个对象保存到永久存储设备上称为持久化

 

一个对象要想能够实现序列化,必须实现java.io.Serializable接口或Externalizable接口

 

Serializable接口没有定义任何方法,是一个标识性接口(Marker Interface,当一个类实现了该接口,就表示这个类的对象是可以序列化的。

 

序列化的特点:

1.当一个对象被序列化时,只保存对象的非静态成员变量,不能保存任何的成员方法和静态变量。

2.如果一个对象的成员变量是一个对象,那么这个对象的数据成员也会被保存。

3.如果一个可序列化的对象包含对某个不可序列化的对象的引用,那么整个序列化操作会失败,并且会抛出一个NotSerializableException.我们可以将这个引用标记为transient,那么对象仍然可以序列化。

 

和序列化相关的两个接口:ObjectInput    ObjectOutput

两个比较重要的类:ObjectInputStream     ObjectOutputStream

 

public class SerializableTest

{

    public static void main(String[] args) throws Exception

    {

       People p1 = new People(20, "jack", 170);

       People p2 = new People(28, "bob", 185);

       People p3 = new People(26, "mark", 165);

 

       FileOutputStream fos = new FileOutputStream("people.txt");

 

       ObjectOutputStream oos = new ObjectOutputStream(fos);

 

       oos.writeObject(p1);

       oos.writeObject(p2);

       oos.writeObject(p3);

 

       oos.close();

 

       FileInputStream fis = new FileInputStream("people.txt");

 

       ObjectInputStream ois = new ObjectInputStream(fis);

 

       People p = null;

 

       for (int i = 0; i < 3; i++)

       {

           p = (People) ois.readObject();

           System.out.println(p.age + "," + p.name + "," + p.height);

       }

    }

}

 

class People implements Serializable

{

    int age;

    transient String name;//transientstatic修饰的成员变量不会被写入到文件中

    double height;

 

    //任何方法也不会被写入到文件中

    public People(int age, String name, double height)

    {

       this.age = age;

       this.name = name;

       this.height = height;

    }

}

 

 

在序列化和反序列化进程中需要特殊处理的Serializable类应该实现以下方法:

private void writeObject(java.io.ObjectOutputStream out) throws Exception{}

private void readObject(java.io.ObjectInputStream in) throws Exception{}

这两个方法不属于任何一个类和任何一个接口,是非常特殊的方法。

 

可以用这两个方法更底层和细粒化的方式控制序列化。

public class SerializableTest2

{

    public static void main(String[] args) throws Exception

    {

       People2 p1 = new People2(20, "jack", 170);

       People2 p2 = new People2(28, "bob", 185);

       People2 p3 = new People2(26, "mark", 165);

 

       FileOutputStream fos = new FileOutputStream("People2.txt");

 

       ObjectOutputStream oos = new ObjectOutputStream(fos);

 

       oos.writeObject(p1);

       oos.writeObject(p2);

       oos.writeObject(p3);

 

       oos.close();

 

       FileInputStream fis = new FileInputStream("People2.txt");

 

       ObjectInputStream ois = new ObjectInputStream(fis);

 

       People2 p = null;

 

       for (int i = 0; i < 3; i++)

       {

           p = (People2) ois.readObject();

           System.out.println(p.age + "," + p.name + "," + p.height);

       }

    }

}

 

class People2 implements Serializable

{

    int age;

    String name;// transientstatic修饰的成员变量不会被写入到文件中

    double height;

 

    // 任何方法也不会被写入到文件中

    public People2(int age, String name, double height)

    {

       this.age = age;

       this.name = name;

       this.height = height;

    }

 

    private void writeObject(java.io.ObjectOutputStream out) throws Exception

    {

       out.writeInt(age);

       out.writeUTF(name);

       System.out.println("write object");

    }

 

    private void readObject(java.io.ObjectInputStream in) throws Exception

    {

       age = in.readInt();

       name = in.readUTF();

       System.out.println("read object");

    }

}

 

原文地址:https://www.cnblogs.com/zfc2201/p/2143644.html