序列化和反序列化

一、序列化

学生类Student
需要实现Serializable接口,否则运行会报错,提示Student无法被序列化

package serializable_32;

import java.io.Serializable;

public class Student implements Serializable{

	private int age;
	private String name;
	private String sex;
	private String password;
	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;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public Student(int age, String name, String sex, String password) {
		super();
		this.age = age;
		this.name = name;
		this.sex = sex;
		this.password = password;
	}
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	
}

序列化Student的对象

package serializable_32;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class SeriaStu {

	public static void main(String[] args) throws FileNotFoundException {
		// TODO Auto-generated method stub

		Student student=new Student(18, "安娜", "女", "123456");
		
		//对象输出流
		ObjectOutputStream oos=null;
		FileOutputStream fos=null;
		
		try {
			fos=new FileOutputStream("serializable.txt");
			oos=new ObjectOutputStream(fos);
			
			//实例对象序列化
			oos.writeObject(student);
			} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				oos.close();
				fos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}

运行结果
在这里插入图片描述

二、反序列化

还是使用原来的Student类,修改SeriaStu类如下,对student进行序列化和反序列化操作

package serializable_32;

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

public class SeriaStu {

	public static void main(String[] args) throws FileNotFoundException {
		// TODO Auto-generated method stub

		Student student=new Student(18, "安娜", "女", "123456");
		
		//对象输出流
		ObjectOutputStream oos=null;
		FileOutputStream fos=null;
		
		//对象输入流
		ObjectInputStream ois=null;
		FileInputStream fis=null;
		
		try {
			//构建对象输出流,为序列化做准备
			fos=new FileOutputStream("serializable.txt");
			oos=new ObjectOutputStream(fos);
			
			//构建对象输入流, 为反序列化做准备
			fis=new FileInputStream("serializable.txt");
			ois=new ObjectInputStream(fis);
			
			//实例对象序列化
			oos.writeObject(student);
			//实现对象反序列化
			Student student2=(Student)ois.readObject();
			System.out.println("反序列化的学生信息:"+student2.getName()+"-"+student2.getAge()+
					"-"+student2.getSex()+"-"+student2.getPassword());
			} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally {
			try {
				ois.close();
				fis.close();
				oos.close();
				fos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}

运行结果
在这里插入图片描述

三、避免序列化和反序列化

1.使用transient修饰Student中的属性,使其避免被序列化和反序列化

//避免密码字段被序列化和反序列化
private transient String password;

2.运行结果如下
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值