java編程思想 序列化

import java.io.*;

public class Ans {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //test0();
        //test1();
        //test2();
        //test3();
        //test4();
        test5();
    }

    static void test0() throws IOException, ClassNotFoundException {
        //序列化可以讓我們擁有保存對象到文件的能力
        //序列化實現Serializable的對象不會調用構造函數,但是要保證可以找到.class文件
        class B {                                                       //沒有實現Serializable的類出現在序列化中會報錯
            B() {System.out.println("New B");}
            B(int i) {System.out.println("New B With I");}
        }

        class C{
            C() {System.out.println("New C");}
        }

        class A extends C implements Serializable {                     //如果父類沒有實現Serializable,在重新導入時會調用默認構造函數
            A(int x,String s,A a) {this.x=x;this.s=s;this.a=a; }

            @Override
            public String toString() {
                return "A{" +
                        "x=" + x +
                        ", s='" + s + '\'' +
                        ", a=" + a +
                        '}';
            }

            //! B b = new B(1);
            int x;
            String s;
            A a;
        }

        A a = new A(0,"a",null);
        a = new A(1,"c",a);
        a = new A(2,"d",a);
        a = new A(3,"e",a);
        a = new A(4,"f",a);
        System.out.println(a);

        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("test"));
        oos.writeObject(a);
        oos.close();
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test"));
        a = (A)ois.readObject();
        System.out.println(a);
    }

    static void test1() throws IOException, ClassNotFoundException {
        //如果想控制序列化行爲,就需要使用Externalizable然後實現writeExternal和readExternal
        class A implements Externalizable {
            public A() {System.out.println("New A");}                       //Externalizable會的調用默認構造器構造對象,然後使用readExternal,所以默認構造器必須是存在的而且是public

            A(int x,String s,A a) {this.x=x;code=s;this.a=a; }
            public void writeExternal(ObjectOutput oo) throws IOException {
                oo.writeInt(x);
                oo.writeObject(a);
            }

            public void readExternal(ObjectInput oi) throws IOException, ClassNotFoundException {
                x = oi.readInt();
                a = (A) oi.readObject();
                code = "no";
            }

            @Override
            public String toString() {
                return "A{" +
                        "x=" + x +
                        ", code='" + code + '\'' +
                        ", a=" + a +
                        '}';
            }

            int x;
            String code;
            A a;
        }

        A a = new A(0,"a",null);
        a = new A(1,"c",a);
        a = new A(2,"d",a);
        a = new A(3,"e",a);
        a = new A(4,"f",a);
        System.out.println(a);

        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("test"));
        oos.writeObject(a);
        oos.close();
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test"));
        a = (A)ois.readObject();
        System.out.println(a);
    }

    static void test2() throws IOException, ClassNotFoundException {
        //有些屬性不想傳輸出去,可以使用transient關鍵字
        class A implements Serializable {
            A(int x,String s,A a) {this.x=x;code=s;this.a=a; }

            @Override
            public String toString() {
                return "A{" +
                        "x=" + x +
                        ", code='" + code + '\'' +
                        ", a=" + a +
                        '}';
            }

            int x;
            transient String code;
            A a;
        }

        A a = new A(0,"a",null);
        a = new A(1,"c",a);
        a = new A(2,"d",a);
        a = new A(3,"e",a);
        a = new A(4,"f",a);
        System.out.println(a);

        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("test"));
        oos.writeObject(a);
        oos.close();
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test"));
        a = (A)ois.readObject();
        System.out.println(a);
    }

    static void test3() throws IOException,ClassNotFoundException {
        //如果不想使用Externalizable,可以使用Serializable然後添加writeObject,readObject,這裏添加的兩個兩個方法不是繼承而來也不是實現接口中的方法,其實只是兩個普通的方法,只不過在運行時ObjectInputStream和ObjectOutputStream會通過反射調用他們
        class A implements Serializable {
            A(int x,String s,A a) {this.x=x;code=s;this.a=a; }

            private void writeObject(ObjectOutputStream oos) throws IOException {
                oos.writeInt(x);
                oos.defaultWriteObject();                                       //通過這個先進行正常的保存,通過反射來實現
                oos.writeObject("tts");
            }

            private void readObject(ObjectInputStream ois) throws IOException,ClassNotFoundException {
                System.out.println(ois.readInt());
                ois.defaultReadObject();
                String s = (String)ois.readObject();
                System.out.println(s+"*");
            }

            @Override
            public String toString() {
                return "A{" +
                        "x=" + x +
                        ", code='" + code + '\'' +
                        ", a=" + a +
                        '}';
            }

            int x;
            String code;
            A a;
        }

        A a = new A(0,"a",null);
        a = new A(1,"c",a);
        a = new A(2,"d",a);
        a = new A(3,"e",a);
        a = new A(4,"f",a);
        System.out.println(a);

        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("test"));
        oos.writeObject(a);
        oos.close();
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test"));
        a = (A)ois.readObject();
        System.out.println(a);
    }

    static void test4() throws IOException,ClassNotFoundException {
        class B implements Serializable {B() {System.out.println("New B");}}

        class A implements Serializable {
            A(int x,String s,A a,B b) {this.x=x;code=s;this.a=a;this.b=b; }

            @Override
            public String toString() {
                return "A{" +
                        "x=" + x +
                        ", code='" + code + '\'' +
                        ", a=" + a +
                        ", b=" + b +
                        '}';
            }

            int x;
            String code;
            A a;
            B b;
        }

        A a = new A(0,"a",null,new B());
        a = new A(1,"c",a,new B());
        a = new A(2,"d",a,new B());
        a = new A(3,"e",a,new B());
        a = new A(4,"f",a,new B());

        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("test"));
        ObjectOutputStream oos2 = new ObjectOutputStream(new FileOutputStream("test2"));
        oos.writeObject(a);
        oos.writeObject(a);
        oos2.writeObject(a);
        oos.close();
        oos2.close();

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test"));
        ObjectInputStream ois2 = new ObjectInputStream(new FileInputStream("test2"));
        a = (A)ois.readObject();
        A a2 = (A)ois.readObject();
        A a3 = (A)ois2.readObject();
        System.out.println(a);                                      //同一個對象存入同一個流兩次,拿出後的結果只會創建一個實例,而放入不同的流兩次,拿出後會創建兩個不同的實例
        System.out.println(a2);
        System.out.println(a3);
    }

    static void test5() throws IOException,ClassNotFoundException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("test"));
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test"));
        X x = new X();
        oos.writeObject(x);
        x = (X) ois.readObject();
        System.out.println(x);                              //如果在別的文件下這個x中俄的static屬性無法被保存,所以需要編寫額外的代碼將其存入
    }

    static void test6() {
        //書中還降到了用XML和Preference來實現序列化,但是由於講的比較少,在這裏先不搞了,以後會深入探究,同時這兩方法是不限於java語言的
    }
}

class X implements Serializable {
    @Override
    public String toString() {
        return String.valueOf(x);
    }

    static int x = 10;
}

 

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