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;
}

 

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