一个对象需要可实现序列化需要满足以下条件.
1.实现Serializable接口
2.父类必须实现可序列化或者存在默认无参构造函数.
3.类的域变量必须实现可序列化或者定义为transient类型.
4.类内部定义 private static final long serialVersionUID = 9999L;(可选)
如果未显式定义serialVersionUID的话,后台会自动定义.
显式定义serialVersionUID的好处是当对象反序列化的时候会匹配原始的serialVersionUID,如果相等则允许可反序列化
可以定义手动的序列化和反序列化.在类定义
private void readObject(ObjectInputStream in)throws ClassNotFoundException,IOException{
in.defaultReadObject();
//to do personal code ...
}
private void writeObject(ObjectOutputStream out)throws IOException{
out.defaultWriteObject();
//to do personal code ...
}
展示代码.
import java.io.Serializable;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class Person implements Serializable{
private String name;
private int age;
private final static long serialVersionUID = 900L;
public Person(String name,int age){
this.name = name;
this.age = age;
}
public void setName(String name){
this.name = name;
}
public void setAge(int age){
this.age = age;
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
private void readObject(ObjectInputStream in)throws IOException,ClassNotFoundException{
in.defaultReadObject();
System.out.println("read:" + this);
}
private void writeObject(ObjectOutputStream out)throws IOException{
out.defaultWriteObject();
System.out.println("write:" + this);
}
public String toString(){
return String.format("name = %s,age = %d",name,age);
}
}
import java.io.ObjectOutputStream;
import java.io.IOException;
import java.io.BufferedOutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import static java.nio.file.StandardOpenOption.*;
public class SerializePerson{
public static void main(String[] args){
Path file = Paths.get(System.getProperty("user.dir")).resolve("file.bin");
try{
if(Files.exists(file)){
Files.delete(file);
}
}catch(IOException e){
e.printStackTrace();
return;
}
try(ObjectOutputStream out = new ObjectOutputStream(
new BufferedOutputStream(Files.newOutputStream(file,WRITE,CREATE)));){
Person person = new Person("Ricky",28);
out.writeObject(person);
out.reset();//这里需要调用reset()方法.清空ObjectOutputStream的对象记忆.否则将只记忆首个对象.
person.setName("Jennie");
person.setAge(27);
out.writeObject(person);
}catch(IOException e){
e.printStackTrace();
return;
}
}
}
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.ObjectInputStream;
import java.io.IOException;
import static java.nio.file.StandardOpenOption.*;
import java.io.EOFException;
import java.io.BufferedInputStream;
public class DeserializePerson{
public static void main(String[] args){
Path file = Paths.get(System.getProperty("user.dir")).resolve("file.bin");
assert Files.exists(file);
try(ObjectInputStream in = new ObjectInputStream(
new BufferedInputStream(Files.newInputStream(file,READ)));){
Person person = null;
while(true){
person = (Person)in.readObject();
}
}catch(EOFException e){
System.out.println("EOF get end of file:" + file);
}catch(ClassNotFoundException|IOException e){
e.printStackTrace();
return;
}
}
}