目录
注意:下面的单个对象和多个对象的序列化和反序列化实现步骤完全相同,只是在多个对象用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();
}
}