将对象从ObjectInputStream转换为存在对象

时间:2016-09-09 16:05:05

标签: java objectinputstream

我正在尝试将对象从ObjectInputStream转换为我班级中已存在的对象:

//main class
Tester tester = new Tester();
tester.setHeight(50);
tester.setWidth(100);

/*FileOutputStream fs = new FileOutputStream("data.txt");
  ObjectOutputStream os = new ObjectOutputStream(fs);
  os.writeObject(tester);
  os.close();*/

FileInputStream fis = new FileInputStream("data.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
Tester x = (Tester) ois.readObject();
System.out.println(x.getHeight() + " " + x.getWidth());

我得到了:

  

java.io.InvalidClassException:Tester;本地类不兼容:   stream classdesc serialVersionUID error。

0 个答案:

没有答案