Java基础--序列化Serializable

时间:2023-03-09 07:53:48
Java基础--序列化Serializable

对Java对象序列化的目的是持久化对象或者为RMI(远程方法调用)传递参数和返回值。

下面是一个序列化对象写入文件的例子:

----------------------------

 package utils;

 import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable; //同样需要实现Serializable
class Data implements Serializable {
private static final long serialVersionUID = 3502676600783498362L;
private String name;
public Data(String name){this.name=name;}
public String toString(){return "d:"+name;}
} //Serializable 为序列化标记接口,不包括任何方法
class User implements Serializable {
//序列化标识,用于标记类是否发生变化
private static final long serialVersionUID = -6950896768312602572L;
private transient String id; //transient关键字可以阻止该字段被序列化
private String name;
private int age;
private Data data; //包含另一个对象
public static String kind="human"; //static 字段不可序列化
public User(String id, String name, int age){
this.id=id;
this.name=name;
this.age=age;
this.data=new Data(name);
}
@Override
public String toString(){
return "["+id+"-"+name+"-"+age+"-"+kind+"-"+data+"]";
}
} public class SerialUtil {
//序列化会暴露对象所有信息(包括private),所以要对敏感信息加密后再序列化
public static void main(String[] args) throws IOException, ClassNotFoundException {
serialize(); //序列化
deSerialize(); //反序列化
} public static void serialize() throws IOException {
User bean1 = new User("1", "lg", 123);
User bean2 = new User("2", "ss", 23);
//ObjectOutputStream为对象输出流,输出到users文件中,名称后缀随意
ObjectOutputStream oo = new ObjectOutputStream(new FileOutputStream(new File("users")));
oo.writeObject(bean1); //将对象序列化到输出流中
oo.writeObject(bean2); //写入另一个
oo.close();
} //如果远程jvm中没有User.class,将抛出ClassNotFoundException
public static void deSerialize() throws IOException, ClassNotFoundException {
//使用ObjectInputStream对象输入流读取文件,反序列化对象
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("users")));
User bean1 = (User) ois.readObject(); //通过二进制构造对象,并不是通过构造函数
User bean2 = (User) ois.readObject(); //读取下一个
ois.close();
System.out.println(bean1);
System.out.println(bean2);
} }

----------------------------

输出结果:

[null-lg-123-human-d:lg]

[null-ss-23-human-d:ss]

如果需要进一步定制序列化反序列化过程,可通过实现接口Externalizable,实现writeExternal和readExternal方法,下面是一个例子:

---------------------------------

 package demos;

 import java.io.Externalizable;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream; public class SerializeBean implements Externalizable{
private int i;
private String s;
private String ss; //从运行结果可见反序列化需要调用该默认构造器,如果为private则会抛出InvalidClassException
public SerializeBean(){
System.out.println("SerializeBean default constractor.");
} public SerializeBean(int i, String s, String ss){
this.i=i;
this.s=s;
this.ss=ss;
} @Override
public String toString(){
return "["+i+"-"+s+"-"+ss+"]";
} //序列化过程中自动调用
@Override
public void writeExternal(ObjectOutput out) throws IOException {
System.out.println("writeExternal here");
out.writeObject(s); //此处添加需要序列化的属性
out.writeInt(i);
} //反序列化过程中自动调用
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
System.out.println("readExternal here");
s=(String)in.readObject(); //此处添加自定义处理逻辑
i=in.readInt();
} public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
SerializeBean bean1 = new SerializeBean(99, "iamstring","ss");
SerializeBean bean2 = new SerializeBean(-44, "string too", "ss"); //序列化到文件
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("out"));
oos.writeObject(bean1);
oos.writeObject(bean2);
oos.close();
bean1=null;
bean2=null; //从文件反序列化
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("out"));
bean1 = (SerializeBean)ois.readObject();
bean2 = (SerializeBean)ois.readObject();
System.out.println(bean1);
System.out.println(bean2);
} }

---------------------------------

输出:

writeExternal here

writeExternal here

SerializeBean default constractor.

readExternal here

SerializeBean default constractor.

readExternal here

[99-iamstring-null]

[-44-string too-null]

ss并未序列化,可见起值为null