IO流 六、對象的序列化與反序列化

6-1 序列化的基本操作

對象的序列化,反序列化

1)對象序列化就是將Object對象轉化成byte序列,反之叫對象的反序列化

2)序列化流(ObjectOutputStream),是個過濾流 --- writeObjcet

      反序列化流(ObjectInputStream), --- readObject

3)序列化接口(Serializable)

對象必須實現序列化接口,才能進行序列化,否則將出現異常

這個接口,沒有任何方法,只是一個標準

package com.io;

import java.io.Serializable;

public class Student implements Serializable{
    private String stuno;
    private String stuname;
    private int stuage;
    public Student() {    
    }
    
    public Student(String stuno, String stuname, int stuage) {
        super();
        this.stuno = stuno;
        this.stuname = stuname;
        this.stuage = stuage;
    }
    @Override
    public String toString() {
        return "Student [stuno=" + stuno + ", stuname=" + stuname + ", stuage=" + stuage + "]";
    }
}
 

package com.io;

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

public class ObjectSeriaDemo1 {

    public static void main(String[] args) throws IOException, ClassNotFoundException {

        String file = "demo/obj.dat";
        // 1.對象的序列化
        /*
         * ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
         * Student stu = new Student("1001", "張三", 20); oos.writeObject(stu);
         * oos.flush(); oos.close();
         */
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
        Student stu = (Student) ois.readObject();
        System.out.println(stu);

    }

}
 

 

6-2 transient及ArrayList源碼分析

private transient int stuage;// 該元素不會進行jvm默認的序列化,也可以自己完成這個元素的序列化

transient關鍵字

private void writeObject(java.io.ObjectOutputStream s) throws IOException {
        s.defaultWriteObject(); // 把jvm能默認序列化的元素進行序列化操作
        s.writeInt(stuage); // 自己完成stuage的序列化
    }

private void readObject(java.io.ObjectInputStream s) throws IOException, ClassNotFoundException {
        s.defaultReadObject(); // 把jvm能默認反序列化的元素進行反序列化操作
        this.stuage = s.readInt(); // 自己完成stuage的反序列化操作
    }

分析ArrayList源碼中序列化和反序列化的問題

  • 有效元素序列化,提高性能

 

6-3 序列化中子父類構造函數問題

package com.io;

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

public class ObjectSeriaDemo {

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

        // ObjectOutputStream oos = new ObjectOutputStream(new
        // FileOutputStream("demo/obj1.dat"));
        // Foo2 foo2 = new Foo2();
        // oos.writeObject(foo2);
        // oos.flush();
        // oos.close();

        // 反序列化是否遞歸調用父類構造函數
        // ObjectInputStream ois = new ObjectInputStream(new
        // FileInputStream("demo/obj1.dat"));
        // Foo2 foo2 = (Foo2)ois.readObject();
        // System.out.println(foo2);
        // ois.close();
        // 輸出結果:com.io.Bar1@34340fab

        // ObjectOutputStream oos = new ObjectOutputStream(new
        // FileOutputStream("demo/obj1.dat"));
        // Bar1 bar1 = new Bar1();
        // oos.writeObject(bar1);
        // oos.flush();
        // oos.close();

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("demo/obj1.dat"));
        Bar1 bar1 = (Bar1) ois.readObject();
        System.out.println(bar1);
        ois.close();
        // 輸出結果:bar com.io.Bar1@34340fab

        /*
         * 對子類對象進行反序列化操作時 如果其父類沒有實現序列化接口 那麼其父類的構造函數會被調用
         */

    }

}

/*
 * 一個類實現了序列化接口,其子類都可以序列化
 */
class Foo implements Serializable {
    public Foo() {
        System.out.println("foo……");
    }
}

class Foo1 extends Foo {
    public Foo1() {
        System.out.println("foo1……");
    }
}

class Foo2 extends Foo1 {
    public Foo2() {
        System.out.println("foo2……");
    }
}

class Bar {
    public Bar() {
        System.out.println("bar");
    }
}

class Bar1 extends Bar implements Serializable {
    public Bar1() {
        System.out.println("bar1");
    }
}
 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章