序列化和反序列化

目录

1.什么是序列化和反序列化

 2.单个对象的序列化。

2.1实体类

2.2序列化代码

2.3反序列化代码

3.多个对象序列化操作

3.1实体类

3.2序列化代码

3.3反序列化代码


注意:下面的单个对象和多个对象的序列化和反序列化实现步骤完全相同,只是在多个对象用list集合而已。

1.什么是序列化和反序列化

简单的说就是把内存中对象一段段拆分过去储存到硬盘中

 2.单个对象的序列化。

首先创建实体类的时候一定要实现Serializable接口

2.1实体类

import java.io.Serializable;

public class Student implements Serializable{//Serializable这个接口只是一个标志没有啥作用,让java虚拟机认识他是要序列化的
	

	private  String name;
//如果给name加上private transient String name;表示不参加序列化
	
	public Student() {
		super();
	}

	public Student(String name) {
	super();
	this.name = name;
}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "Student [name=" + name + "]";
	}
}

2.2序列化代码

序列化代码主要是由ObjectOutputStream类实现的配合writeObject方法将内容存储进FileOutputStream这个路径中。

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

public class SerializeOutputObjet{
	public static void main(String[] args) throws FileNotFoundException, IOException {
		
		Student student=new Student();
		student.setName("zhangsan");
		//序列化
		ObjectOutputStream objectInputStream=new ObjectOutputStream(new FileOutputStream("students"));
		
		objectInputStream.writeObject(student);
		objectInputStream.flush();
		objectInputStream.close();
		
	}	


}

2.3反序列化代码

序列化代码主要是由ObjectInputStream 类实现的配合readObject方法将FileInputStream目录下的内容读取

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 SerializeInputObjet{
	public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
		//反序列化步骤
		ObjectInputStream objectInputStream=new ObjectInputStream(new FileInputStream("students"));
		Object readObject = objectInputStream.readObject();
		System.out.println(readObject);
		objectInputStream.close();
		
	}	
}

3.多个对象序列化操作

3.1实体类

同上

3.2序列化代码

就是将单个对象存储的数组中。

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
//一次序列化多个对向用List集合储存
public class SerializeOutputObjetList{
	public static void main(String[] args) throws FileNotFoundException, IOException {
		
		List<Student> s=new ArrayList<Student>();
		s.add(new Student("zahngsan1"));
		s.add(new Student("zahngsan2"));
		s.add(new Student("zahngsan3"));

		ObjectOutputStream objectOutputStream=new ObjectOutputStream(new FileOutputStream("studentList"));
		objectOutputStream.writeObject(s);
		
		objectOutputStream.flush();
		objectOutputStream.close();
	
	}	
}

3.3反序列化代码

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.util.ArrayList;
import java.util.List;
//一次反序列化多个对向用List集合储存
public class SerializeInputObjetList{
	public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
		
		ObjectInputStream objectInputStream=new ObjectInputStream(new FileInputStream("studentList"));
		List<Student> list = (List<Student>)objectInputStream.readObject();
	
		for (Student student : list) {
			System.out.println(student);
		}
		objectInputStream.close();
	}	
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值