一、实现 Serializable 接口
1、具体使用
public class SerializableDemo implements Serializable {
private String name;
private Integer age;
private transient String address; //被标记为Transient 表示不被序列化
//序列化唯一标识
private static final long serialVersionUID = 1L;
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public Integer getAge(){
return age;
}
public void setAge(Integer name){
this.age = age;
}
public String getAddress(){
return address;
}
public void setAge(String address){
this.address = address;
}
}
//main函数
public static void main(string[] args){
//序列化
SerializableDemo serdemo = new SerializableDemo();
serdemo.setName("张三");
serdemo.setAge(10);
serdemo.setAddress("北京");
ObjectOutputStream output;
try{
output = new ObjectOutputStream(new FileOutputStream("tempFile"));
output.write(serdemo);
}catch(IOException e){
e.printStreak();
}finally{
if(output!=null)
output.close();
}
//反序列化
ObjectInputStream oInput;
try{
oInput= new ObjectInputStream(new FileInputStream("tempFile"));
SerializableDemo demo = (SerializableDemo )oInput.readObject(serdemo);
System.out.println(demo.getName()); //输出 张三
Systm.out.println(demo.getAddress()); //输出 null 被标记为Transient 表示不被序列化
}catch(IOException e){
e.printStreak();
}finally{
if(output!=null)
output.close();
}
}
注意点:
1、序列化并不保存静态变量
2、Transient 关键字的作用是控制变量的序列化
3、父类要想被序列化,需要父类也实现 Serializable 接口
4、自定义策略 重写Serializable 接口中的方法 writeObject和readObject方法
二、实现 Externalizable 接口
public class ExternalizableDemo implements Externalizable {
private String name;
private Integer age;
private transient String address; //被标记为Transient 表示不被序列化
//序列化唯一标识
private static final long serialVersionUID = 1L;
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public Integer getAge(){
return age;
}
public void setAge(Integer name){
this.age = age;
}
public String getAddress(){
return address;
}
public void setAge(String address){
this.address = address;
}
public void writeExternal(ObjectOutput out) throws IOException {
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
}
}
//main函数
public static void main(string[] args){
//序列化
ExternalizableDemo serdemo = new ExternalizableDemo();
serdemo.setName("张三");
serdemo.setAge(10);
serdemo.setAddress("北京");
ObjectOutputStream output;
try{
output = new ObjectOutputStream(new FileOutputStream("tempFile"));
output.write(serdemo);
}catch(IOException e){
e.printStreak();
}finally{
if(output!=null)
output.close();
}
//反序列化
ObjectInputStream oInput;
try{
oInput= new ObjectInputStream(new FileInputStream("tempFile"));
SerializableDemo demo = (SerializableDemo )oInput.readObject(serdemo);
System.out.println(demo.getName()); //输出 null 没有被序列化出来
System.out.println(demo.getAge()); //输出 null 没有被序列化出来
Systm.out.println(demo.getAddress()); //输出 null 被标记为Transient 表示不被序列化
}catch(IOException e){
e.printStreak();
}finally{
if(output!=null)
output.close();
}
}
Externalizable继承了Serializable,该接口中定义了两个抽象方法:writeExternal()
与readExternal()
。当使用Externalizable接口来进行序列化与反序列化的时候需要开发人员重写writeExternal()
与readExternal()
方法
public class ExternalizableDemo implements Externalizable {
private String name;
private Integer age;
private String address;
//序列化唯一标识
private static final long serialVersionUID = 1L;
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public Integer getAge(){
return age;
}
public void setAge(Integer name){
this.age = age;
}
public String getAddress(){
return address;
}
public void setAge(String address){
this.address = address;
}
public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(name);
out.writeInt(age);
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
name = (String) in.readObject();
age = in.readInt();
}
}
//main函数
public static void main(string[] args){
//序列化
ExternalizableDemo serdemo = new ExternalizableDemo();
serdemo.setName("张三");
serdemo.setAge(10);
serdemo.setAddress("北京");
ObjectOutputStream output;
try{
output = new ObjectOutputStream(new FileOutputStream("tempFile"));
output.write(serdemo);
}catch(IOException e){
e.printStreak();
}finally{
if(output!=null)
output.close();
}
//反序列化
ObjectInputStream oInput;
try{
oInput= new ObjectInputStream(new FileInputStream("tempFile"));
SerializableDemo demo = (SerializableDemo )oInput.readObject(serdemo);
System.out.println(demo.getName()); //输出 张三
System.out.println(demo.getAge()); //输出 李四
Systm.out.println(demo.getAddress()); //输出 null
}catch(IOException e){
e.printStreak();
}finally{
if(output!=null)
output.close();
}
}
如果序列化类中没有无参数的构造函数,在运行时会抛出异常:java.io.InvalidClassException