Java中序列化和反序列化

序列化机制可以将对象转换成字节序列,这些字节序列可以保存到磁盘上,也可以在网络中传输,并且程序可以将这些字节序列恢复成原来的对象。其中,序列化(Serialize)是指将一个Java对象写入IO流中,而反序列化(Deserialize)则是指从IO流中恢复这个Java对象。

如果对象要支持序列化机制,则它的类需要实现Serializable接口。该接口只是一个标记接口,它没有提供任何方法,只是用来标明该类是可以序列化的。在Java中很多类都已经实现了Serializable接口,比如包装类、String、Date等。

在实现序列化和反序列化的时候,我们需要使用ObjectInputStream和ObjectOutputStream这两个

LineIterator it = FileUtils.lineIterator(theFile, "UTF-8");
try {
    while (it.hasNext()) {
        String line = it.nextLine();
        // do something with line
    }
} finally {
    LineIterator.closeQuietly(it);
}

流。其中,序列化时可以通过ObjectOutputStream的writeObject()输出对象序列,反序列化时可以通过ObjectInputStream的readObject()将序列恢复为对象。

序列化的目的是将对象中的数据(成员变量)转为字节序列,和成员方法无关。为了正确地序列化某个对象,需要这个对象的类符合如下规则:

  1. 该对象中引用类型的成员变量也必须是可序列化的。

  2. 该类的直接或间接的父类,要么具有无参构造器,要么也是可序列化的。

  3. 一个对象只会被序列化一次,再次序列化时仅仅会输出它的序列号而已。

【延伸阅读】

序列化与反序列化的示例如下:

/**
 * 序列化
 */

public class IODemo {
    public static void main(String[] args) {
        testSerialize();
        testDeserialize();
    }

 
    public static void testSerialize() {
        try (
                ObjectOutputStream oos = new ObjectOutputStream(
                        new FileOutputStream("C:/work/alpha/car.txt"));
        ) {
            oos.writeObject(new Car("奔驰", "红色", 300));
            oos.writeObject(new Car("宝马", "蓝色", 400));
            oos.writeObject(new Car("奥迪", "黑色", 500));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

public static void testDeserialize() {
        try (
                ObjectInputStream ois = new ObjectInputStream(
                        new FileInputStream("C:/work/alpha/car.txt"));
        ) {
            System.out.println(ois.readObject());
            System.out.println(ois.readObject());
            System.out.println(ois.readObject());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
 

}

 
class Car implements Serializable {
 

    private String brand;
    private String color;
    private int maxSpeed;
 

    public Car(String brand, String color, int maxSpeed) {
        System.out.println("Init Car.");
        this.brand = brand;
        this.color = color;
        this.maxSpeed = maxSpeed;
    }



    public String getBrand() {
        return brand;
    }

 

    public void setBrand(String brand) {
        this.brand = brand;
    }

 
    public String getColor() {
        return color;
    }
    
	public void setColor(String color) {
        this.color = color;
    }
 

    public int getMaxSpeed() {
        return maxSpeed;
    }


    public void setMaxSpeed(int maxSpeed) {
        this.maxSpeed = maxSpeed;
    }

 
    @Override
    public String toString() {
        return "Car{" +
                "brand='" + brand + '\'' +
                ", color='" + color + '\'' +
                ", maxSpeed=" + maxSpeed +
                '}';
    }
 

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冲冲冲冲冲冲!!!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值