对象序列化

  • Java序列化将那些实现了Serializable接口的对象转换成一个字节序列,并能够将字节序列恢复成原来的对象。
  • 利用它可以实现轻量化的持久性。
  • 对象序列化加入到语言中是为了支持两种主要特性。一是Java远程调用,它使存活于其它计算机上的对象使用起来就向存活于本机一样。当向远程发送数据时,需要序列化来传送参数和返回值。
  • 只要对象实现了Serializable接口,对象序列化出理就会非常简单。
  • ObjectOuputStream ObjectInputStream

对象序列化demo

package com.zachary.io.serializable;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Random;

class Data implements Serializable {
	private static final long serialVersionUID = 1L;
	private int n;
	public Data(int n) {
		this.n = n;
	}

	@Override
	public String toString() {
		return Integer.toString(n);
	}
}

/**
 * @author Zachary.Zheng
 * @version 1.0
 * @date 2020年6月3日 上午9:39:52
 */
public class Worm implements Serializable {
	private static final long serialVersionUID = 1L;
	private static Random rand = new Random(47);
	private Data[] d = { new Data(rand.nextInt(10)), new Data(rand.nextInt(10)), new Data(rand.nextInt(10)) };
	private Worm next;
	private char c;
	public Worm(int i, char x) {
		System.out.println("Worn constructor: " + i);
		c = x;
		if(--i>0) {
			next = new Worm(i, (char)(x + 1));
		}
	}
	public Worm() {
		System.out.println("Defult constructor");
	}
	@Override
	public String toString() {
		StringBuilder result = new StringBuilder(":");
		result.append(c);
		result.append("(");
		for(Data data: d) {
			result.append(data);
		}
		result.append(")");
		
		if(next != null) {
			result.append(next);
		}
		return result.toString();
	}
	public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
		Worm w = new Worm(6, 'a');
		System.out.println("w = " + w);
		File file = new File("file/output/serializable");
		file.mkdirs();
		// 写到文件中
		ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("file/output/serializable/worm.out"));
		out.writeObject("Worm storage\n");
		out.writeObject(w);
		out.close();
		
		ObjectInputStream in = new ObjectInputStream(new FileInputStream("file/output/serializable/worm.out"));
		String s = (String) in.readObject();
		Worm w2 = (Worm) in.readObject();
		System.out.println(s + "w2 = " + w2);
		in.close();
		
		// 写到数组中
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		ObjectOutputStream out2 = new ObjectOutputStream(bout);
		out2.writeObject("Worm storage\n");
		out2.writeObject(w2);
		out2.flush();
		out2.close();
		
		ObjectInputStream in2 = new ObjectInputStream(new ByteArrayInputStream(bout.toByteArray()));
		s = (String) in2.readObject();
		Worm w3 = (Worm) in2.readObject();
		System.out.println(s + "w3 = " + w3);
		in2.close();
		
	}
}

Output:
Worn constructor: 6
Worn constructor: 5
Worn constructor: 4
Worn constructor: 3
Worn constructor: 2
Worn constructor: 1
w = :a(853):b(119):c(802):d(788):e(199):f(881)
Worm storage
w2 = :a(853):b(119):c(802):d(788):e(199):f(881)
Worm storage
w3 = :a(853):b(119):c(802):d(788):e(199):f(881)

寻找类

在其他没有Worm类的项目中反序列化,因为Java虚拟机找不到Worm类,会等到一个ClassNotFoundException异常。正常执行必须保持虚拟机能找到相关的.class文件。

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;

public class ThawWrom {
    public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
        ObjectInputStream in = new ObjectInputStream(new FileInputStream("E:/Work/javaBean/javaBeanLearn/file/output/serializable/worm.out"));
        String s = (String) in.readObject();
        Object w2 = in.readObject();
        System.out.println(s + "w2 = " + w2);
        in.close();
    }
}

Output:
Exception in thread “main” java.lang.ClassNotFoundException: com.zachary.io.serializable.Worm

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值