序列化指将内存中的对象通过网络或管道的方式保存到硬盘文件的过程。反序列化指将硬盘文件中的对象通过网络或管道的方式恢复到内存的过程。在序列化与反序列的过程中,不会一次性将整个对象传过去,而是将要传输的对象分为多份,并将其编号,按序号一份一份传输。序列化可以理解为拆分对象,反序列化可以理解为组装对象。
- 序列化:
package com.pyi.blog.blog15; import java.io.Serializable; //创建一个商品类 public class Good implements Serializable { private String name; private int price; public Good() { } public Good(String name, int price) { this.name = name; this.price = price; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } @Override public String toString() { return "Good{" + "name='" + name + ''' + ", price=" + price + '}'; } }package com.pyi.blog.blog15; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; public class SerializeTest { public static void main(String[] args) { //创建Good类的对象,对其进行序列化 Good good=new Good("可乐",3); //创建一个对象字节输入流,使用它来进行序列化操作 ObjectOutputStream out=null; try { out=new ObjectOutputStream(new FileOutputStream("Hello")); //使用对象字节输出流中的writeObject()方法来对对象进行序列化 try { out.writeObject(good); } catch (IOException e) { e.printStackTrace(); } //刷新流 out.flush(); } catch (IOException e) { e.printStackTrace(); }finally{ //关闭对象字节输出流 if(out!=null){ try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } } }运行结果如下:
此时程序发生了NotSerializableException(不支持序列化异常)。错误的原因是Good类的对象不支持序列化。参与序列化与反序列化的对象必须实现Serializable接口。
而从Serializable接口的源代码中可以看出它没有任何方法等代码,是一个空接口。在Java中,它被称为标志性接口,只有标识的作用。在运行的过程中,JVM遇见实现Serializable接口的类后,若没有手动定义序列化版本号,JVM会为此类自动生成一个序列化版本号。
package com.pyi.blog.blog15;
import java.io.Serializable;
//商品类实现Serializable接口
public class Good implements Serializable{
private String name;
private int price;
public Good() {
}
public Good(String name, int price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return "Good{" +
"name='" + name + ''' +
", price=" + price +
'}';
}
}
运行结果如下:
此时程序序列化成功!
- 反序列化
package com.pyi.blog.blog15;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
public class SerializableTest {
public static void main(String[] args) {
//创建对象字节输入流对象,使用它进行反序列化操作
ObjectInputStream in=null;
try {
in=new ObjectInputStream(new FileInputStream("Hello"));
//运用readObject()方法进行反序列化
Object o=in.readObject();
System.out.println(o);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally{
//关闭对象字节输入流
if(in!=null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
运行结果如下:
序列化集合参与序列化与反序列化的集合都需要实现Serializable接口。
package com.pyi.blog.blog15;
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ListSerializableTest {
public static void main(String[] args) {
//创建ArrayList集合对象,并赋给List类的变量list
List list=new ArrayList<>();
//添加多个集合元素
list.add(new Good("可乐",3));
list.add(new Good("牛奶",5));
list.add(new Good("果汁",4));
ObjectOutputStream out=null;
ObjectInputStream in=null;
//创建对象字节输出流,用其进行集合对象的序列化操作
try {
out=new ObjectOutputStream(new FileOutputStream("Hello"));
//创建对象字节输入流,用其进行集合对象的反序列化操作
in=new ObjectInputStream(new FileInputStream("Hello"));
//进行序列化
out.writeObject(list);
//进行反序列化
Object obj=in.readObject();
//若obj对象是List集合,则将其强转为List集合
if(obj instanceof List){
List l=(List) obj;
//使用迭代器遍历List集合
Iterator itr= l.iterator();
while (itr.hasNext()){
System.out.println(itr.next());
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
//关闭对象字节输出流
if(out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}if(in!=null){
//关闭对象字节输入流
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
运行结果如下:
- transient关键字
transient关键字指游离不参与序列化,若一些属性不需要序列化,可用transient关键字修饰。
package com.pyi.blog.blog15;
import java.io.Serializable;
public class Good implements Serializable{
//在名字前面加上transient关键字,则名字不会参与序列化与反序列化
private transient String name;
private int price;
public Good() {
}
public Good(String name, int price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return "Good{" +
"name='" + name + ''' +
", price=" + price +
'}';
}
}
运行结果如下:
此时名字的值为空。
- 序列化版本号
一个类实现了Serializable接口后进行了序列化,JVM自动生成了序列化版本号后,若很久之后此类的源代码改动了之后进行了重新编译,生成了新的字节码文件,则其再次运行时,JVM会为其生成一个与原来序列化版本号不同的一个新的序列化版本号。若再次对其对象进行反序列化操作,则会出错误。
package com.pyi.blog.blog15;
import java.io.Serializable;
public class Good implements Serializable{
//为商品类新添加一个id属性,改动了商品类的源代码
private int id;
private transient String name;
private int price;
public Good() {
}
public Good(String name, int price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return "Good{" +
"name='" + name + ''' +
", price=" + price +
'}';
}
}
运行结果如下:
在类名相同的情况下,JVM通过序列化版本号区分类,此时虽为同一个类,但是因为改动了该类的源代码,JVM为该类生成了新的序列化版本号,序列化版本号与原来的不一致,因此JVM认为它们不是同一个类,无法进行反序列化。
因此实现类Serializable接口后,可以手动为该类定义一个序列化版本号,此时JVM便不会为其自动生成序列化版本号了。若该的该类的源代码,序列化版本化也不会发生变化。
package com.pyi.blog.blog15;
import java.io.Serializable;
public class Good implements Serializable{
//为商品类手动定义了一个序列化版本号
private static final long serialVersionUID=1234567891234567891L;
private int id;
private transient String name;
private int price;
public Good() {
}
public Good(String name, int price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return "Good{" +
"name='" + name + ''' +
", price=" + price +
'}';
}
}
为商品类手动定义了一个序列化版本号后,对其对象进行序列化。之后改变该类的源代码。
package com.pyi.blog.blog15;
import java.io.Serializable;
//去掉了id属性
public class Good implements Serializable{
private static final long serialVersionUID=1234567891234567891L;
private transient String name;
private int price;
public Good() {
}
public Good(String name, int price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return "Good{" +
"name='" + name + ''' +
", price=" + price +
'}';
}
}
此时改动了源代码,去掉了id属性。之后对其进行反序列化,运行结果如下:
此时反序列化成功!
若一个类不需要改动源代码,则可以不手动定义序列化版本号。若其需要改动源代码则应该在该类中手动定义序列化版本号。



