Java--对象流、序列化和反序列化

本文介绍Java中对象序列化和反序列化的原理及应用,包括如何使用ObjectOutputStream和ObjectInputStream进行对象的序列化与反序列化,以及在序列化过程中需要注意的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、对象流、序列化与反序列化

1. 使用对象流可以实现对象的序列化和反序列化操作

相关类

ObjectOutputStream (用于序列化)  输出

ObjectInputStream(用于反序列化)  输入

2.为什么要序列化: 易于保存,易于传输

3.序列化和反序列化的过程:

序列化是将对象的状态存储到特定存储介质中的过程(将对象转换成字节序列(二进制)的过程)

反序列化则是从特定存储介质中的数据重新构建对象的过程(将对象的二进制形式转换成对象)


4.序列化的步骤


5.反序列化步骤:

例题:序列化

package serial;
import java.io.Serializable;
public class Person implements Serializable{
	/**
	 * 添加一个控制序列化与反序列化版本一致性的serialVersionUID属性
	 */
	private static final long serialVersionUID = 1L;
	private String name;
	private int age;
	private transient String sex;   // 由transient修饰的属性不能被序列化保存
	static String job;   // 静态属性也不能被序列化保存	
	public Person() {
	}
	public Person(String name, int age,String sex) {
		super();
		this.name = name;
		this.age = age;
		this.sex=sex;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]"+job;
	}	
}
package serial;
import java.io.*;
public class SerialDemo {
	public static void main(String[] args) {
		File f=new File("c:"+File.separator+"person.txt");
		Person per=new Person("胡夫",80,"男");
		OutputStream out=null;
		ObjectOutputStream oos=null;
		try {
			out=new FileOutputStream(f);
			oos=new ObjectOutputStream(out);  
			Person.job="皇帝";
			oos.writeObject(per);   // 序列化
			System.out.println("序列化成功!");
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				oos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

运行结果:


例题:反序列化

package serial;
import java.io.*;
public class DeSerialDemo {
	public static void main(String[] args) {
		File f=new File("c:"+File.separator+"person.txt");
		InputStream input=null;
		ObjectInputStream ois=null;
		try {
			input=new FileInputStream(f);
			ois=new ObjectInputStream(input);
			Person per=(Person)ois.readObject();    // 反序列化
			System.out.println(per);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				ois.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

运行结果:

package serial;
import java.io.*;
import java.util.*;
public class ListSerial {
	public static void main(String[] args) {
		File f=new File("c:"+File.separator+"person.txt");
		List<Person> list=new ArrayList<Person>();
		Collections.addAll(list,new Person("张三",20,"男"),new Person("李四",25,"男"),new Person("王娟",26,"女"));
		OutputStream out=null;
		ObjectOutputStream oos=null;
		try {
			out=new FileOutputStream(f);
			oos=new ObjectOutputStream(out);  
			oos.writeObject(list);   // 序列化
			System.out.println("序列化成功!");
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				oos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

运行结果:


package serial;
import java.util.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;

public class ListDeSerial {
	public static void main(String[] args) {
		File f=new File("c:"+File.separator+"person.txt");
		InputStream input=null;
		ObjectInputStream ois=null;
		try {
			input=new FileInputStream(f);
			ois=new ObjectInputStream(input);
			List<Person> list=(List<Person>)ois.readObject();    // 反序列化
			for(Person per:list){
				System.out.println(per);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				ois.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

运行结果:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值