Serializable

#对象序列化Object Serialization

实现有限持久化(必须在自己的程序中明确地序列化和组装对象)

为序列化一个对象,首先要创建某些OutputStream 对象,然后将其封装到 ObjectOutputStream 对象内
只需调用writeObject()即可完成对象的序列化,并将其发送给OutputStream

相反的过程是将一个InputStream 封装到 ObjectInputStream 内,然后调用 readObject()
最后获得的是指向一个上溯造型Object 的句柄,所以必须下溯造型,以便能够直接设置

对象网
对象序列化特别“聪明”的一个地方是它不仅保存了对象的“全景图”,而且能追踪对象内包含的所有句柄并保存那些对 象; 接着又能对每个对象内包含的句柄进行追踪

在对一个Serializable(可序列化)对象进行重新装配的过程中,不会调用任何构建器(甚至默认构建器)。整个对象都是 过从InputStream 中取得数据恢复的

package com.zy.chapter10;

import java.io.*;

class Cat implements Serializable {
    private String catName;

    public Cat(String name) {
        this.catName = name;
    }

    public String toString() {
        return catName;
    }
}

class Room implements Serializable {
    private int roomNumber;

    public Room nextRoom;

    private static String name() {
        return "" + (int) (Math.random() * 10);
    }

    private Cat[] cats = { new Cat(name()), new Cat(name()), new Cat(name()) };

    Room(int i) {
        this.roomNumber = i;
        if (i < 5) {
            this.nextRoom = new Room(i + 1);
        }
    }

    public String toString() {

        String s = " cats: ";
        for (int i = 0; i < cats.length; i++) {
            s += cats[i].toString() + " ";
        }
        return "roomNumber:" + this.roomNumber + s;
    }
}

/**
 * @ClassName SerializableDemo.java
 * @function Test Serializable function
 * @author yiz
 * @version V1.0
 * @date 2018年1月11日
 */
public class SerializableDemo {
    public static void main(String[] args) {
        Room room = new Room(1);
        String filePath = "test.txt";
        // write object
        try {
            ObjectOutputStream objouts = new ObjectOutputStream(new FileOutputStream(filePath));
            objouts.writeObject(room);
            objouts.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // read object
        try {
            ObjectInputStream objins = new ObjectInputStream(new FileInputStream(filePath));
            // need Downcasting
            Room room2 = (Room) objins.readObject();
            // show Objects
            while (room2 != null) {
                System.out.println(room2);
                room2 = room2.nextRoom;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println();
        // write object
        try {
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            ObjectOutputStream objouts = new ObjectOutputStream(bout);
            objouts.writeObject(room);
            objouts.close();
            ObjectInputStream objins = new ObjectInputStream(new ByteArrayInputStream(bout.toByteArray()));
            // need Downcasting
            Room room2 = (Room) objins.readObject();
            // show Objects
            while (room2 != null) {
                System.out.println(room2);
                room2 = room2.nextRoom;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
roomNumber:1 cats: 0 0 1 
roomNumber:2 cats: 0 9 2 
roomNumber:3 cats: 3 9 7 
roomNumber:4 cats: 1 2 7 
roomNumber:5 cats: 7 5 8 

roomNumber:1 cats: 0 0 1 
roomNumber:2 cats: 0 9 2 
roomNumber:3 cats: 3 9 7 
roomNumber:4 cats: 1 2 7 
roomNumber:5 cats: 7 5 8 
恢复了一个序列化的对象后,如果想对其做更多的事情,必须保证JVM 能在本地类路径或者因特网的其他什么地方找到相关的.class 文件

package com.zy.chapter10;

import java.io.Serializable;

public class Tree implements Serializable {
}
package com.zy.chapter10;

import java.io.*;

public class PersistenceTree {
    public static void main(String[] args) throws Exception {
        ObjectOutput out = new ObjectOutputStream(new FileOutputStream("file.x"));
        Tree zorcon = new Tree();
        out.writeObject(zorcon);
    }
}

package com.zy.chapter10;

import java.io.*;

public class ReadTree {
    public static void main(String[] args) throws Exception {
        ObjectInputStream in = new ObjectInputStream(new FileInputStream("file.x"));
        Object mystery = in.readObject();
        System.out.println(mystery.getClass().toString());
    }
}

class com.zy.chapter10.Tree


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值