一个demo快速理解序列化和反序列化

代码示例

分享一个例子用来快速理解序列化和反序列化
其实序列化和反序列化就是为了交换数据,(简单粗暴的理解就是把运行中的对象存进文件里面)

import java.io.*;

public class Main {
    public static void main(String[] args) throws Exception{
        System.out.println("________________java序列化________________");
        System.out.println("[+]new一个对象小李");
        person XiaoLi=new person(18,"小李");

        System.out.println("[+]调用XiaoLi的say方法");
        XiaoLi.say();

        System.out.println("[+]进行序列化\n    对象XiaoLi -> 文件XiaoLi.ser");
        FileOutputStream fileout=new FileOutputStream("XiaoLi.ser");
        ObjectOutputStream objout=new ObjectOutputStream(fileout);
        objout.writeObject(XiaoLi);
        objout.close();

        System.out.println("[+]进行反序列化\n   文件XiaoLi.ser -> 对象people");
        FileInputStream filein=new FileInputStream("XiaoLi.ser");
        ObjectInputStream objin=new ObjectInputStream(filein);
        person people=(person)objin.readObject();
        System.out.println("[+]调用people的say方法");
        people.say();


        System.out.println("________________java序列化-嵌套+自定义序列化方法________________");
        AppleTree appleTree=new AppleTree(5,"红苹果","苹果树");
        appleTree.say();

        System.out.println("[+]进行序列化");
        FileOutputStream fileout2=new FileOutputStream("AppleTree.ser");
        ObjectOutputStream objout2=new ObjectOutputStream(fileout2);
        objout2.writeObject(appleTree);
        objout2.close();

        System.out.println("[+]进行反序列化");
        FileInputStream filein2=new FileInputStream("AppleTree.ser");
        ObjectInputStream objin2=new ObjectInputStream(filein2);
        AppleTree tree=(AppleTree) objin2.readObject();
        System.out.println("[+]调用tree的say方法");
        tree.say();

    }
}

class person implements Serializable{
    private int age;
    private String name;

    public person(int age,String name){
        System.out.println("[+]person构造方法");
        this.age=age;
        this.name=name;
    }

    public void say(){
        System.out.println("    我是'"+this.name+"'今年"+this.age+"岁了");
    }
}




class AppleTree implements Serializable{
    private Apple apple;
    private transient int  id;//transient修饰的字段默认不序列化
    private String TreeName;

    public AppleTree(int num,String name,String TreeName){
        this.id=6;
        System.out.println("[+]AppleTree构造方法");
        this.TreeName=TreeName;
        this.apple=new Apple(num,name);
    }
    private void readObject (ObjectInputStream objin)throws Exception{
        System.out.println("    AppleTree的readObject处理");
        //objin.defaultReadObject();
        System.out.println("    AppleTree的readObject处理apple");
        this.apple=(Apple) objin.readObject();
        System.out.println("    AppleTree的readObject处理TreeName");
        this.TreeName=(String) objin.readObject();
    }
    private void writeObject(ObjectOutputStream objout)throws Exception{
        System.out.println("    AppleTree的writeObject");
        //objout.defaultWriteObject();
        System.out.println("    AppleTree的writeObject处理apple");
        objout.writeObject(this.apple);
        System.out.println("    AppleTree的writeObject处理TreeName");
        objout.writeObject(this.TreeName);
    }
    public void say(){
        System.out.println("    我是'"+this.TreeName);
        this.apple.say();
    }
}





class Apple implements Serializable{
    private int num;
    private String name;

    public Apple(int num,String name){
        System.out.println("[+]Apple构造方法");
        this.num=num;
        this.name=name;
    }
    private void readObject (ObjectInputStream objin)throws Exception{
        System.out.println("        Apple的readObject");
        //objin.defaultReadObject();
        System.out.println("        Apple的readObject处理name");
        this.name=(String) objin.readObject();
        System.out.println("        Apple的readObject处理num");
        this.num= (int) objin.readObject();
    }
    private void writeObject(ObjectOutputStream objout)throws Exception{
        System.out.println("        Apple的writeObject");
        //objout.defaultWriteObject();
        System.out.println("        Apple的writeObject处理name");
        objout.writeObject(this.name);
        System.out.println("        Apple的writeObject处理num");
        objout.writeObject(this.num);
    }
    public void say(){
        System.out.println("    我是'"+this.name+"'一共有"+this.num+"个");
    }
}

运行结果

image

image

运行结果

posted @ 2024-05-02 16:59  aixve  阅读(35)  评论(0)    收藏  举报