理解Java序列化

序列化的理解

序列化:把对象转换为字节序列的过程称为对象的序列化。
反序列化:把字节序列恢复为对象的过程称为对象的反序列化。

序列化目的:为了让不同JVM之间共享实例对象

作用:保存类对象某个时刻下的状态 (快照) ,让其可以多次复用或者是在其他JVM上恢复(还原)

特点
1.序列化将对象数据保存为一组字节数组,并且可以通过反序列化复原
2. 仅保存某个时刻的成员变量,而并不关注静态变量
3. 序列化对象的引用链的对象·

应用:保存对象快照,远程方法调用,网络传输对象


序列化的使用

1.Serializable 接口

  • 该接口会自动对成员变量进行序列化
public class Student implements Serializable{
	private int id;
	private String name;
    //通过transient关键字可以定义变量不被序列化
	private transient char sex; 
	
	public Student(int id, String name, char sex) {
		super();
		this.id = id;
		this.name = name;
		this.sex = sex;
	}
	
	public int getId() {
		return id;
	}
	public String getName() {
		return name;
	}
	public char getSex() {
		return sex;
	}
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", sex=" + sex + "]";
	}
	
}
public static void main(String[] args) throws FileNotFoundException, IOException {
		File file = new File("student");
		ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));
		out.writeObject(new Student(20162590, "Reformat", 'M'));
		System.out.println("输出成功");
    
    	ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
		Student s =(Student) in.readObject();
		System.out.println(s);
		out.close();
	}
输出成功
反序列化:Student [id=20162590, name=Reformat, sex= ]

2.Externalizable接口

  • 这个接口需要自己实现writeExternal readExternal 里面的内容来确定哪些变量需要序列化
public class Student implements Externalizable{
	private int id;
	private String name;
	private char sex;
	
	public Student(int id, String name, char sex) {
		super();
		this.id = id;
		this.name = name;
		this.sex = sex;
	}
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public char getSex() {
		return sex;
	}
	public void setSex(char sex) {
		this.sex = sex;
	}
	

	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", sex=" + sex + "]";
	}

	@Override
	public void writeExternal(ObjectOutput out) throws IOException {
		out.writeInt(id);
		out.writeObject(name);
		out.writeObject(sex);
	}

	@Override
	public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
		id = in.readInt();
		name = (String) in.readObject();
		sex = (char) in.readObject();
	}
	
}
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
		Student s = new Student(20162590, "Reformat", 'M');
		
		File file = new File("student2");
		ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));
		s.writeExternal(out);
		System.out.println("输出为:"+s);
		
		s.setId(20162516);s.setSex('X');
		System.out.println("更改为:"+s);
		
		ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
		s.readExternal(in);
		System.out.println("还原后:"+s);
		out.close();
	}
结果为:
输出为:Student [id=20162590, name=Reformat, sex=M]
更改为:Student [id=20162516, name=Reformat, sex=X]
还原后:Student [id=20162590, name=Reformat, sex=M]

你可能感兴趣的:(java基础,java,序列化)