不写serialVersionUID的测试

1.写一个类实现serializable接口,提示没有定义serialVersionUID,先造一个对象test然后写入文件.

 

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;

import antlr.CharBuffer;

public class SerialTest implements Serializable {
 /**
  *
  */
// private static final long serialVersionUID = -9067733772326927784L;
 public int i;
 public int y;
 //public int z;
 private SerialTest test;

 public void write() {
  try {
   ObjectOutputStream out = new ObjectOutputStream(
     new FileOutputStream("d://text.txt"));
   SerialTest test = new SerialTest();
   test.i = 1;
   test.y = 2;

   out.writeObject(test);
   out.close();
  } catch (Exception ex) {
   ex.printStackTrace();
  }
 }

 public void read() {
  try {
   ObjectInputStream in = new ObjectInputStream(new FileInputStream(
     "d://text.txt"));

   SerialTest test = (SerialTest) in.readObject();

   System.out.println("original object is " + test.i + "@" + test.y);
   in.close();
  } catch (Exception ex) {
   ex.printStackTrace();
  }

 }

 public static void main(String[] args) {
 
  SerialTest test = new SerialTest();
  test.i = 2;
  test.y = 1;
  test.write();
  //test.read();
 }

}

 

2.对类进行一点更改,加一个int z; 然后从文件反序列化此对象,报异常

 

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;

import antlr.CharBuffer;

public class SerialTest implements Serializable {
 /**
  *
  */
// private static final long serialVersionUID = -9067733772326927784L;
 public int i;
 public int y;
 public int z;
 private SerialTest test;

 public void write() {
  try {
   ObjectOutputStream out = new ObjectOutputStream(
     new FileOutputStream("d://text.txt"));
   SerialTest test = new SerialTest();
   test.i = 1;
   test.y = 2;

   out.writeObject(test);
   out.close();
  } catch (Exception ex) {
   ex.printStackTrace();
  }
 }

 public void read() {
  try {
   ObjectInputStream in = new ObjectInputStream(new FileInputStream(
     "d://text.txt"));

   SerialTest test = (SerialTest) in.readObject();

   System.out.println("original object is " + test.i + "@" + test.y);
   in.close();
  } catch (Exception ex) {
   ex.printStackTrace();
  }

 }

 public static void main(String[] args) {
 
  SerialTest test = new SerialTest();
  test.i = 2;
  test.y = 1;
//  test.write();
  test.read();
 }

}

 

java.io.InvalidClassException: SerialTest; local class incompatible: stream classdesc serialVersionUID = -9067733772326927784, local class serialVersionUID = 7085551939486009872
 at java.io.ObjectStreamClass.initNonProxy(Unknown Source)
 at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
 at java.io.ObjectInputStream.readClassDesc(Unknown Source)
 at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
 at java.io.ObjectInputStream.readObject0(Unknown Source)
 at java.io.ObjectInputStream.readObject(Unknown Source)
 at SerialTest.read(SerialTest.java:40)
 at SerialTest.main(SerialTest.java:56)

 

就是说,前面序列化的时候系统自动把一个反映当时类的serialVersionUID写入了文件,第二次类改变了,

反序列化的时候,发现当前类的serialVersionUID和文件中的serialVersionUID不同,于是报出异常,解决方法,

给现在的类手工写入一个serialVersionUID = -9067733772326927784L,程序working.

 

result:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;

import antlr.CharBuffer;

public class SerialTest implements Serializable {
 /**
  *
  */
 private static final long serialVersionUID = -9067733772326927784L;
 public int i;
 public int y;
 public int z;
 private SerialTest test;

 public void write() {
  try {
   ObjectOutputStream out = new ObjectOutputStream(
     new FileOutputStream("d://text.txt"));
   SerialTest test = new SerialTest();
   test.i = 1;
   test.y = 2;

   out.writeObject(test);
   out.close();
  } catch (Exception ex) {
   ex.printStackTrace();
  }
 }

 public void read() {
  try {
   ObjectInputStream in = new ObjectInputStream(new FileInputStream(
     "d://text.txt"));

   SerialTest test = (SerialTest) in.readObject();

   System.out.println("original object is " + test.i + "@" + test.y);
   in.close();
  } catch (Exception ex) {
   ex.printStackTrace();
  }

 }

 public static void main(String[] args) {
 
  SerialTest test = new SerialTest();
  test.i = 2;
  test.y = 1;
//  test.write();
  test.read();
 }

}

 

因此如果实现Serializable接口的类,要么自己定义一个serialVersionUID,要么默认开发环境Eclipse会生成

一个serialVersionUID,这样当类做修改时,依然会成功地在不同版本代码间完成序列化和反序列化

 

 

 

 

 

 

 

 

 


 

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 技术工厂 设计师:CSDN官方博客 返回首页