首页 > 代码库 > Java IO学习总结(三)

Java IO学习总结(三)

一、对象序列化

        序列化就是把内存中的java对象转换成与平台无关的二进制流,从而把这种二进制流保存在磁盘,或者通过网络传输到另一个网络节点;一旦获取到这个二进制流也可以恢复成Java对象。序列化可以使对象脱离程序的运行而独立存在。

        Java对象序列化,则该类必须实现Serializable接口或者Externalizable接口。

        示例代码

public class SerializableTest {
    public static void main(String[] args) {
        try {
            // 序列化
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("d:\\demo.txt"));
            Demo demo = new Demo(18, "test");
            oos.writeObject(demo);
            
            // 反序列化
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("d:\\demo.txt"));
            Demo de = (Demo)ois.readObject();
            System.out.println("age:" + de.getAge() + ";name:" + de.getName());
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        
    }
}

public class Demo implements Serializable{
    private int age;
    private String name;
    
    public Demo(int age, String name) {
        super();
        this.age = age;
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

二、对象引用序列化

Java序列化对象时,引用的对象也必须实现Serializable接口,Java序列化机制是:所以保存在磁盘的对象都有一个序列化编号,当程序视图序列化一个对象时,首先检查该对象是否被序列化,如果已经被序列化,程序则直接输出一个序列化编号,否则进行序列化。

示例代码

public class SerializableTest {
    public static void main(String[] args) {
        try {
            // 序列化
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("d:\\demo.txt"));
            Demo demo = new Demo(18, "test");
            Demo1 demo1 = new Demo1("test1",demo);
            Demo1 demo2 = new Demo1("test2",demo);
            oos.writeObject(demo);
            oos.writeObject(demo1);
            oos.writeObject(demo2);
            oos.writeObject(demo1);
            
            // 反序列化
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("d:\\demo.txt"));
            Demo de = (Demo)ois.readObject();
            Demo1 de1 = (Demo1)ois.readObject();
            Demo1 de2 = (Demo1)ois.readObject();
            Demo1 de3 = (Demo1)ois.readObject();
            System.out.println(de==de1.getDemo());
            System.out.println(de==de2.getDemo());
            System.out.println(de1==de3);
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

public class Demo1 implements Serializable{
    private String name;
    private Demo demo;
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Demo getDemo() {
        return demo;
    }
    public void setDemo(Demo demo) {
        this.demo = demo;
    }
    public Demo1(String name, Demo demo) {
        super();
        this.name = name;
        this.demo = demo;
    }
    
}

结果是:

true
true
true

Java IO学习总结(三)