序列化和反序列化

序列化和反序列

作用

在java中把对象转化成字节序列的过程称为序列化

把对象从字节序列恢复为对象的过程称为反序列化

应用场景

1.序列化和反序列化可以用于把对象的字节序列化永久的存储到硬盘中(通常存放在一个文件中)

2.在网络上传送对象字节序列

使用

实现Serializable接口,即可使用

1.我们创建一个Serial类,把Serial类进行序列化和反序列化,序列化到E盘下会生成一个.txt文件里面保存了Serial类的信息。如下代码是没有问题的:

package com.zll;

import java.io.*;

public class Person {
   public static void main(String[] args) throws IOException, ClassNotFoundException {
       Serialize();
       Serial s=DeSerialize();


  }
   private static void Serialize() throws IOException {
       Serial s=new Serial(1,1,"zll",15);
       ObjectOutputStream oo=new ObjectOutputStream(new FileOutputStream(new File("E:/Serial.txt")));
       oo.writeObject(s);
       System.out.println("Serial序列化成功!");
       oo.close();
  }
   private static Serial DeSerialize() throws IOException, ClassNotFoundException {
       ObjectInputStream input=new ObjectInputStream(new FileInputStream(new File("E:/Serial.txt")));
       Serial s = (Serial) input.readObject();
       System.out.println("反序列化成功");
       return s;
  }

}
class Serial implements Serializable{
   private int id;
   private String name;
   private int age;
   private int s;
   public Serial(int s,int id,String name,int age){
       this.id=id;
       this.age=age;
       this.name=name;
       this.s=s;
  }
}

 

 

2.此时我们把序列化注释掉,并且进行修改Serial类中的一个值,然后进行Serial类的反序列化。如下:

package com.zll;

import java.io.*;

public class Person {
   public static void main(String[] args) throws IOException, ClassNotFoundException {
       //Serialize();
       Serial s=DeSerialize();


  }
   private static void Serialize() throws IOException {
       Serial s=new Serial(1,1,"zll",15);
       ObjectOutputStream oo=new ObjectOutputStream(new FileOutputStream(new File("E:/Serial.txt")));
       oo.writeObject(s);
       System.out.println("Serial序列化成功!");
       oo.close();
  }
   private static Serial DeSerialize() throws IOException, ClassNotFoundException {
       ObjectInputStream input=new ObjectInputStream(new FileInputStream(new File("E:/Serial.txt")));
       Serial s = (Serial) input.readObject();
       System.out.println("反序列化成功");
       return s;
  }

}
class Serial implements Serializable{
   private int id;
   private String name;
   private int age;
   private int s;
   private int a;//添加一个a属性
   public Serial(int s,int id,String name,int age){
       this.id=id;
       this.age=age;
       this.name=name;
       this.s=s;

  }
}

 

 

出现了一下错误:stream classdesc serialVersionUID = -2106814416120498085, local class serialVersionUID = 1830915755854034503

出现了一个我们没有定义的属性serialVersionUID,虽然我们没有进行赋值,但是会自动的给serialVersionUID赋值.因为第一次进行序列化时UID已经有一个值了。如果我们不修改Serial类中的内容然后进行反序列化也是不会报错的,但是我们在Serial类中添加了一个属性。UID就不同了所以报了错误。

serialVersionUID有两种生成方式:

private static final long serialVersionUID=1L;//第一种
private static final long serialVersionUID=4603642343377807741L;//第二种是根据类名方法名属性接口来生产的。

3.加上serialVersionUID后程序是否会报错

package com.zll;

import java.io.*;

public class Person {
   public static void main(String[] args) throws IOException, ClassNotFoundException {
       Serialize();
       Serial s=DeSerialize();


  }
   private static void Serialize() throws IOException {
       Serial s=new Serial(1,1,"zll",15);
       ObjectOutputStream oo=new ObjectOutputStream(new FileOutputStream(new File("E:/Serial.txt")));
       oo.writeObject(s);
       System.out.println("Serial序列化成功!");
       oo.close();
  }
   private static Serial DeSerialize() throws IOException, ClassNotFoundException {
       ObjectInputStream input=new ObjectInputStream(new FileInputStream(new File("E:/Serial.txt")));
       Serial s = (Serial) input.readObject();
       System.out.println("反序列化成功");
       return s;
  }

}
class Serial implements Serializable{
   private static final long serialVersionUID=1L;
   private int id;
   private String name;
   private int age;
   private int s;
   //private int a;
   public Serial(int s,int id,String name,int age){
       this.id=id;
       this.age=age;
       this.name=name;
       this.s=s;

  }
}
package com.zll;

import java.io.*;

public class Person {
   public static void main(String[] args) throws IOException, ClassNotFoundException {
       //Serialize();
       Serial s=DeSerialize();


  }
   private static void Serialize() throws IOException {
       Serial s=new Serial(1,1,"zll",15);
       ObjectOutputStream oo=new ObjectOutputStream(new FileOutputStream(new File("E:/Serial.txt")));
       oo.writeObject(s);
       System.out.println("Serial序列化成功!");
       oo.close();
  }
   private static Serial DeSerialize() throws IOException, ClassNotFoundException {
       ObjectInputStream input=new ObjectInputStream(new FileInputStream(new File("E:/Serial.txt")));
       Serial s = (Serial) input.readObject();
       System.out.println("反序列化成功");
       return s;
  }

}
class Serial implements Serializable{
   private static final long serialVersionUID=1L;
   private int id;
   private String name;
   private int age;
   private int s;
   private int a;//添加了一个a属性
   public Serial(int s,int id,String name,int age){
       this.id=id;
       this.age=age;
       this.name=name;
       this.s=s;

  }
}

结果:

 

总结:所以一般写序列化时要加上serialVersionUID这样对类进行修改操作时就不会UID不一致导致错误了

posted @ 2021-11-19 21:00  乐Malone  阅读(158)  评论(0)    收藏  举报