最近重新学习java基础 重新整理下关于序列化反序列化的知识
首先java的序列化一般需要实现Serializable接口
package serializable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class SerializableTest implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private transient int age;
private static String nativeCountry;
public static void main(String[] args) throws Exception {
File file = new File("f:" + File.separator + "serializable.txt");
SerializableTest cla = new SerializableTest();
cla.setAge(1);
cla.setName("张三");
nativeCountry = "中国";
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
file));
out.writeObject(cla);
out.close();
ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
SerializableTest get = (SerializableTest) in.readObject();
in.close();
System.out.println(get);
}
@Override
public String toString() {
return "SerializableTest [name=" + name + ", age=" + age
+ ", nativeCountry=" + nativeCountry + "]";
}
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;
}
}
结果
结果发现age并没有序列化到文本中 反而静态变量nativeCountry反而有值,第一个没值很正常 因为有transient关键字 自动序列化时会自动忽略,但是静态变量有值是因为本机请求的 换个机器访问即可,实际上是没有序列化的。
如果不适用自动序列化 可以实现Externalizable 接口 可以自己设定需要序列化的字段 这样 就算有transient 关键字也会序列化
package serializable;
import java.io.Externalizable;
import java.io.File;
import java.io.FileInputStream;
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 ExternalizableTest implements Externalizable {
private static final long serialVersionUID = 1L;
private String name;
private int age;
private static String nativeCountry;
public static void main(String[] args) throws Exception {
File file = new File("f:" + File.separator + "serializable.txt");
ExternalizableTest et = new ExternalizableTest();
et.setName("张三");
et.setAge(10);
ObjectOutput out = new ObjectOutputStream(new FileOutputStream(file));
out.writeObject(et);
ObjectInput in = new ObjectInputStream(new FileInputStream(file));
ExternalizableTest et1 = (ExternalizableTest) in.readObject();
System.out.println(et1);
out.close();
in.close();
}
@Override
public String toString() {
return "SerializableTest [name=" + name + ", age=" + age
+ ", nativeCountry=" + nativeCountry + "]";
}
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;
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(name);
out.writeInt(age);
}
@Override
public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException {
name = (String) in.readObject();
age = in.readInt();
}
}