本文主要是介绍local class incompatible: stream classdesc serialVersionUID = 1134808625626452086,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
罪魁祸首居然是toString
学到java序列化与反序列化时,练习遇到了问题,记录一下:
最开始序列化时成功,能够把car对象写进D://car.txt
是一串乱码…(是否是乱码不重要,关键是写进到文件里去了)
但是当我从car.txt中反序列化,想在控制台打印信息时,就报错了
经过几番测试,发现是toString的问题去掉toString后就OK了
错误代码:
public class JavaSerilizable{public static void main(String[] args) throws IOException, ClassNotFoundException {//Car c = new Car("001","red",20000.0);FileInputStream fin = new FileInputStream("D://car.txt");ObjectInputStream objs = new ObjectInputStream(fin);Car o = (Car)objs.readObject();System.out.println(o.getColor());objs.close();fin.close();}
}class Car implements Serializable{private String id;private String color;private Double price;public Car(String id, String color, Double price) {this.id = id;this.color = color;this.price = price;}public String getId() {return id;}public void setId(String id) {this.id = id;}public String getColor() {return color;}public void setColor(String color) {this.color = color;}public Double getPrice() {return price;}public void setPrice(Double price) {this.price = price;}@Overridepublic String toString() {return "Car{" +"id='" + id + '\'' +", color='" + color + '\'' +", price=" + price +'}';}
}
总结:在此只做记录,等以后有时间再来深挖;
如果有大佬知道原因的,请在评论区留下您的妙言!
这篇关于local class incompatible: stream classdesc serialVersionUID = 1134808625626452086的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!