Java 对象的序列化和反序列化

对象的序列化和反序列化

/*
 * 序列:排队,
 * 把对象转为字节序列,序列化的过程
 * 
 * ObjectOutputStream:用于输出对象,把对象转成字节数据输出,对象的输出过程称为序列化。
 * 		ObjectOutputStream比OutputStream多了很多方法,其中一个是  writeObject(obj)
 * 
 * 只能将支持 java.io.Serializable 接口的对象写入流中。每个 serializable 对象的类都被编码,编码内容包括类名和类签名、对象的字段值和数组值,以及从初始对象中引用的其他所有对象的闭包。 
	writeObject 方法用于将对象写入流中。所有对象(包括 String 和数组)都可以通过 writeObject 写入。可将多个对象或基元写入流中。必须使用与写入对象时相同的类型和顺序从相应 ObjectInputstream 中读回对象。
 *
 *
 * ObjectInputstream:用于输入对象,把字节序列转为对象读取,对象的读取过程称为反序列化。
 * 		ObjectInputstream比InputStream多了很多方法,其中一个是 Object readObject()
 */
public class TestObject {
	@Test
	public void test02()throws IOException, ClassNotFoundException{
		FileInputStream fis = new FileInputStream("obj.dat");
		ObjectInputStream ois = new ObjectInputStream(fis);
		Object obj = ois.readObject();
		//把字节流中的数据,转为一个对象,读取过程中会创建对象,new对象时需要找对象的类型
		System.out.println(obj);
		
		ois.close();
		fis.close();
	}
	
	@Test
	public void test01()throws IOException{
		User u = new User("chailinyan","123456",28);
		
		FileOutputStream fos = new FileOutputStream("obj.dat");
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		
		//数据:程序-->oos --> fos -->obj.dat
		oos.writeObject(u);
		/*
		 * 如果没有实现Serializable接口的类型,在序列化时,报错误NotSerializableException,不能序列化。
		 * 如果要解决问题,User类需要实现java.io.Serializable接口
		 */
		
		oos.close();
		fos.close();
	}
}

序列化版本ID

public class TestObject2 {
	@Test
	public void test02()throws IOException, ClassNotFoundException{
		FileInputStream fis = new FileInputStream("obj.dat");
		ObjectInputStream ois = new ObjectInputStream(fis);
		
		/*
		 * 当对象已经输出到文件中后,修改了类,再次读取这个文件时,报InvalidClassException。
		 * 报这个错的原因是:流中关于类的serialVersionUID与本地类的serialVersionUID对不上,就会报InvalidClassException错误,
		 * 如何解决?
		 * (1)修改本地的serialVersionUID为流中的serialVersionUID
		 * (2)或者,在当初实现Serializable接口时,就固定一个serialVersionUID,这样每次编译就不会自动生成一个新的serialVersionUID
		 */
		Object obj = ois.readObject();
		//把字节流中的数据,转为一个对象,读取过程中会创建对象,new对象时需要找对象的类型
		System.out.println(obj);
		
		ois.close();
		fis.close();
	}
}
/*
 * 自定义异常时:
 * (1)继承Throwable或它的子类
 * 一般是Exception和RuntimeException比较多
 * (2)建议保留两个构造器,一个无参,一个可以message赋值的构造器
 * (3)建议增加serialVersionUID
 */
public class TestException {

}
class MyException extends Exception{

	private static final long serialVersionUID = 1L;

	public MyException() {
		super();
		
	}

	public MyException(String message) {
		super(message);
		
	}
	
}

不序列的字段

/*
 * 需求:
 *   不是对象中的所有属性都需要序列化的。
 *   
 * 如果某个属性不需要序列化,可以在属性的前面加一个关键字:transient 。
 * 如果某个属性是static,那么也不会序列化。因为静态的变量不属于某个对象,而是整个类的,所以不需要随着对象的序列化而序列化。
 */
public class TestObject3 {
	@Test
	public void test02()throws IOException, ClassNotFoundException{
		FileInputStream fis = new FileInputStream("goods.dat");
		ObjectInputStream ois = new ObjectInputStream(fis);
		
		Object obj = ois.readObject();
		System.out.println(obj);
		
		ois.close();
		fis.close();
	}
	@Test
	public void test01()throws IOException{
		Goods.setBrand("atguigu");
		//现在要序列化这个产品的信息,但是希望销量100不序列化
		Goods g = new Goods("裙子",88.8,100);
		
		FileOutputStream fos = new FileOutputStream("goods.dat");
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		
		oos.writeObject(g);
		
		oos.close();
		fos.close();
	}
}

对象的引用数据类型属性都要实现Serializable

public class TestObject4 {
	@Test
	public void test02(){
		Husband h = new Husband();
		Wife wife = new Wife();
		
		h.setName("崔志恒");
		wife.setName("石榴");
		
		h.setWife(wife);
		wife.setHusband(h);
		
		System.out.println(h);
	}
	
	@Test
	public void test01()throws Exception {
		Husband h = new Husband();
		Wife wife = new Wife();
		
		h.setName("崔志恒");
		wife.setName("石榴");
		
		h.setWife(wife);
		wife.setHusband(h);
		
		FileOutputStream fos = new FileOutputStream("marry.dat");
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		
		oos.writeObject(h);
		/*
		 * java.io.NotSerializableException: com.atguigu.test10.Wife
		 * 虽然Husband实现了Serializable,但是因为在序列化Husband过程中,包含wife对象,所以要求Wife类也要实现Serializable接口
		 */
		
		oos.close();
		fos.close();
	}
}



public class Wife {
	private String name;
	private Husband husband;
	public Wife(String name, Husband husband) {
		super();
		this.name = name;
		this.husband = husband;
	}
	public Wife() {
		super();
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Husband getHusband() {
		return husband;
	}
	public void setHusband(Husband husband) {
		this.husband = husband;
	}
	@Override
	public String toString() {
		return "Wife [name=" + name + ", husband=" + husband.getName() +  "]";
	}
	
}



/*
 * 在序列化Husband对象,要求Wife序列化。
 * 这里发现String类型也实现序列化接口了。
 * 
 * 结论:
 * 	序列化一个对象时,要求它的属性要么是基本数据类型,如果是引用数据类型,这个引用数据类型也必须实现Serializable接口。
 *  序列化一个数组,要求元素类型实现Serializable接口。
 */
public class Husband implements Serializable{
	private static final long serialVersionUID = 1L;
	private String name;
	private Wife wife;
	public Husband(String name, Wife wife) {
		super();
		this.name = name;
		this.wife = wife;
	}
	public Husband() {
		super();
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Wife getWife() {
		return wife;
	}
	public void setWife(Wife wife) {
		this.wife = wife;
	}
	@Override
	public String toString() {
		return "Husband [name=" + name + ", wife=" + wife.getName() + "]";
	}
	
}

序列化接口Externalizable

public class Goods implements Externalizable{
	private static String brand = "尚硅谷";
	private String name;
	private double price;
	private transient int sale;
	public Goods(String name, double price, int sale) {
		super();
		this.name = name;
		this.price = price;
		this.sale = sale;
	}
	public Goods() {
		super();
	}
	public static String getBrand() {
		return brand;
	}
	public static void setBrand(String brand) {
		Goods.brand = brand;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public int getSale() {
		return sale;
	}
	public void setSale(int sale) {
		this.sale = sale;
	}
	@Override
	public String toString() {
		return "Goods [brand = " + brand +",name=" + name + ", price=" + price + ",sale = " + sale +"]";
	}
	@Override
	public void writeExternal(ObjectOutput out) throws IOException {
		//程序员自己定制要序列化的内容,顺序等
		//这两个方法是在对象被序列化和反序列化的过程中,JVM帮我们调用
		out.writeUTF(brand);//静态的也序列化
		out.writeUTF(name);
		out.writeDouble(price);
		out.writeInt(sale);//有transient也序列化
	}
	@Override
	public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
		//程序员自己定制要反序列化的内容,顺序等,建议与序列化的顺序一致
		brand = in.readUTF();
		name = in.readUTF();
		price = in.readDouble();
		sale = in.readInt();
	}
	
}


/*
 * 1、java.io.Serializable接口:
 * 		类通过实现 java.io.Serializable 接口以启用其序列化功能。未实现此接口的类将无法使其任何状态序列化或反序列化。
 * 		可序列化类的所有子类型本身都是可序列化的。序列化接口没有方法或字段,仅用于标识可序列化的语义。
 *   如果实现Serializable接口,对象如何序列化,各个属性序列化的顺序是什么,都是默认的,程序员无法指定,也不用关心。
 *   如果属性前面有static和transient修饰,不参与序列化。
 * 
 * 2、java.io.Externalizable接口:
 *    若某个要完全控制某一对象及其超类型的流格式和内容,则它要实现 Externalizable 接口的 writeExternal 和 readExternal 方法。
 *    程序员在writeExternal方法中,自定定制哪些属性要序列化,顺序是什么样。
 *    程序员在readExternal方法中,自定定制哪些属性要反序列化,顺序和writeExternal的方法中一致。
 */
public class TestObject {
	@Test
	public void test01() throws IOException{
		Goods goods = new Goods("《从入门到放弃》", 99.99, 1000);
		Goods.setBrand("atguigu");
		
		FileOutputStream fos = new FileOutputStream("goods.dat");
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		
		oos.writeObject(goods);
		
		oos.close();
		fos.close();
	}
	
	@Test
	public void test02()throws IOException, ClassNotFoundException{
		FileInputStream fis = new FileInputStream("goods.dat");
		ObjectInputStream ois = new ObjectInputStream(fis);
		
		Object obj = ois.readObject();
		System.out.println(obj);
		
		ois.close();
		fis.close();
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值