> 文档中心 > serialVersionUID变量和Transient方法

serialVersionUID变量和Transient方法


serialVersionUID变量和Transient关键字

  1. 对象序列化序列化了一个对象后,假如我们修改了是对象所属的类文件,读取数据会不会出问题?
    会出问题,抛出InvalidClassException异常
    图文:
    柏维怡我爱你

  2. 当序列化运行时检测到类中的以下问题之一时抛出。

  • 类的串行版本与从流中读取的类描述符的类型不匹配
  • 该类包含未知的数据类型
  • 该类没有可访问的无参数构造函数
  1. 如果出问题了,如何解决呢?
    给对象所属的类加一个serialVersionUID
    private static final long serialVersionUID=42L;
    图文:
    柏维怡我爱你

  2. 如果一个对象中的某个成员变量的值不想被序列化,又该如何实现呢?
    给该成员变量加transient关键字修饰,该关键字标记的成员变量不参与序列化过程

问题分析

代码1(不做修改下)

学生类(此时没有添加任何修改)

package Demo;import java.io.Serializable;public class Student implements Serializable {    private String name;    private int age;    public Student() {    }    public Student(String name, int age) { this.name = name; this.age = age;    }    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;    }}

Demo类

package Demo;import java.io.*;public class Demo {    public static void main(String[] args) throws IOException, ClassNotFoundException { write(); read();    }    //读取文件数据    public static void  read() throws IOException, ClassNotFoundException { //建立对象反序列化 ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\item\\Hellow\\src\\copyJava.txt")); //读取数据 Object o = ois.readObject(); //转换为学生类 Student s=(Student)o; //调用该类的对应的方法 System.out.println(s.getName()+s.getAge()); //释放资源 ois.close();    }    //写入文件数据    public static  void write() throws IOException { //建立对象序列化 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\item\\Hellow\\src\\copyJava.txt")); //建立对象,添加内容 Student student = new Student("菜徐琨",28); //写入数据到对应的文件中 oos.writeObject(student); //释放资源 oos.close();    }}

输出的结果
serialVersionUID变量和Transient方法

代码2(注意了!!!)

学生类(有添加任何修改)!!!

package Demo;import java.io.Serializable;public class Student implements Serializable {    private String name;    private int age;    public Student() {    }    public Student(String name, int age) { this.name = name; this.age = age;    }    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 String toString() { return "Student{" +  "name='" + name + '\'' +  ", age=" + age +  '}';    }}

Demo类

package Demo;import java.io.*;public class Demo {    public static void main(String[] args) throws IOException, ClassNotFoundException { //write(); read();    }    //读取文件数据    public static void  read() throws IOException, ClassNotFoundException { //建立对象反序列化 ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\item\\Hellow\\src\\copyJava.txt")); //读取数据 Object o = ois.readObject(); //转换为学生类 Student s=(Student)o; //调用该类的对应的方法 System.out.println(s.getName()+s.getAge()); //释放资源 ois.close();    }    //写入文件数据    public static  void write() throws IOException { //建立对象序列化 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\item\\Hellow\\src\\copyJava.txt")); //建立对象,添加内容 Student student = new Student("菜徐琨",28); //写入数据到对应的文件中 oos.writeObject(student); //释放资源 oos.close();    }}

输出的内容
serialVersionUID变量和Transient方法

代码3(解决问题)

学生类(新的变动)!!!
serialVersionUID变量和Transient方法

package Demo;import java.io.Serializable;public class Student implements Serializable {    private static final long serialVersionUID =43L;//强制声明    private String name;    private transient int age;    public Student() {    }    public Student(String name, int age) { this.name = name; this.age = age;    }    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 String toString() { return "Student{" +  "name='" + name + '\'' +  ", age=" + age +  '}';    }}

Demo类

package Demo;import java.io.*;public class Demo {    public static void main(String[] args) throws IOException, ClassNotFoundException { write(); read();    }    //读取文件数据    public static void  read() throws IOException, ClassNotFoundException { //建立对象反序列化 ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\item\\Hellow\\src\\copyJava.txt")); //读取数据 Object o = ois.readObject(); //转换为学生类 Student s=(Student)o; //调用该类的对应的方法 System.out.println(s.getName()+s.getAge()); //释放资源 ois.close();    }    //写入文件数据    public static  void write() throws IOException { //建立对象序列化 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\item\\Hellow\\src\\copyJava.txt")); //建立对象,添加内容 Student student = new Student("菜徐琨",28); //写入数据到对应的文件中 oos.writeObject(student); //释放资源 oos.close();    }}

输出的内容
serialVersionUID变量和Transient方法

代码3是代码1和代码2的总结!!!!