什么是Java序列化与反序列化?
1、序列化:Java中的序列化机制能够将一个实例对象信息写入到一个字节流中(只序列化对象的属性值,而不会去序列化方法),序列化后的对象可用于网络传输,或者持久化到数据库、磁盘中。
2、反序列化:需要对象的时候,再通过字节流中的信息来重构一个相同的对象。
3、实现序列化条件
1)实现接口:Serializable标识接口
2)对象所在的类提供常量:序列版本号。
3)要求对象的属性也是可序列化的。(基本数据类型本身是可序列化的),注意被static修饰的、transient关键字修饰的属性是不能被序列化的。
4、代码实现
1、java对象Person
package ObjectInoutOutputTest;
import java.io.Serializable;
public class Person implements Serializable {
/*
1.实现接口Serializable(标识接口)
2.当前类提供一个全局的常量:serialVersionUID
3.除了当前的类需要实现Serializable接口外,还必须保证内部的所有属性也必须是可序列化的。
ObjectOutputStrea和ObjectInputStream不能序列化static和transient修饰的成员变量
*/
public static final long serialVersionUID = 8365573970943482803L;
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
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 Person() {
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
2、序列化
package ObjectInoutOutputTest;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class xuliehua {
/*
序列化过程:将内存中的java对象保存到磁盘中或通过网络传输出去
使用ObjectOutputStream实现
*/
public static void main(String[] args) {
ObjectOutputStream oos=null;
try {
oos = new ObjectOutputStream(new FileOutputStream("object.dat"));
oos.writeObject(new String("我爱北京天安门"));
oos.flush();
oos.writeObject(new Person("小王",23));
oos.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
2、序列化
package ObjectInoutOutputTest;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class xuliehua {
/*
序列化过程:将内存中的java对象保存到磁盘中或通过网络传输出去
使用ObjectOutputStream实现
*/
public static void main(String[] args) {
ObjectOutputStream oos=null;
try {
oos = new ObjectOutputStream(new FileOutputStream("object.dat"));
oos.writeObject(new String("我爱北京天安门"));
oos.flush();
oos.writeObject(new Person("小王",23));
oos.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
3、反序列化
package ObjectInoutOutputTest;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
public class fanxulie {
/*
反序列化:将磁盘文件中的对象还原为内存中的一个java对象
使用ObjectInputStream
*/
public static void main(String[] args) {
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream("object.dat"));
Object obj = ois.readObject();
String str = (String) obj;
Person p = (Person) ois.readObject();
System.out.println(str);
System.out.println(p);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}finally {
if (ois != null) {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
打开writeObject方法的源码看一下,发现方法中有这么一个逻辑,当要写入的对象是String、Array、Enum、Serializable类型的对象则可以正常序列化,否则会抛出NotSerializableException异常。
这就能解释为什么Java序列化一定要实现Serializable接口了。
/**
* Underlying writeObject/writeUnshared implementation.
*/
private void writeObject0(Object obj, boolean unshared)
throws IOException
{
boolean oldMode = bout.setBlockDataMode(false);
depth++;
try {
// 省略号。。。。。。。。。。
// remaining cases
if (obj instanceof String) {
writeString((String) obj, unshared);
} else if (cl.isArray()) {
writeArray(obj, desc, unshared);
} else if (obj instanceof Enum) {
writeEnum((Enum<?>) obj, desc, unshared);
} else if (obj instanceof Serializable) {
writeOrdinaryObject(obj, desc, unshared);
} else {
if (extendedDebugInfo) {
throw new NotSerializableException(
cl.getName() + "\n" + debugInfoStack.toString());
} else {
throw new NotSerializableException(cl.getName());
}
}
} finally {
depth--;
bout.setBlockDataMode(oldMode);
}
}
5、Serializable接口
package java.io;
/**
* @author unascribed
* @see java.io.ObjectOutputStream
* @see java.io.ObjectInputStream
* @see java.io.ObjectOutput
* @see java.io.ObjectInput
* @see java.io.Externalizable
* @since JDK1.1
*/
public interface Serializable {
}
6、既然已经实现了Serializaable接口,为什么还要显示指定serialVersionUID的值?
因为序列化对象时,如果不显示的设置serialVersionUID,Java在序列化时会根据对象属性自动的生成一个serialVersionUID,再进行存储或用作网络传输。
在反序列化时,会根据对象属性自动再生成一个新的serialVersionUID,和序列化时生成的serialVersionUID进行比对,两个serialVersionUID相同则反序列化成功,否则就会抛异常。
而当显示的设置serialVersionUID后,Java在序列化和反序列化对象时,生成的serialVersionUID都为我们设定的serialVersionUID,这样就保证了反序列化的成功
7、transient关键字
序列化对象时如果希望哪个属性不被序列化,则用transient关键字修饰即可。
@Data
public class User implements Serializable {
private transient String name;
private String age;
}
可以看到字段name的值没有被保存到磁盘中,一旦变量被transient修饰,变量将不再是对象持久化的一部分,该变量内容在序列化后无法获得访问。
Java序列化前的结果: User(name=fufu, age=18)
Java反序列化的结果:User(name=null, age=18)
一个静态变量不管是否被transient修饰,均不能被序列化。 因为static修饰的属性是属于类,而非对象