不写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,这样当类做修改时,依然会成功地在不同版本代码间完成序列化和反序列化

 

 

 

 

 

 

 

 

 


 

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:技术工厂 设计师:CSDN官方博客 返回首页

打赏作者

ladofwind

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值