java.io.InvalidClassException异常
当我们使用对象序列化流将对象写入到文件中后修改了对象实体类里的属性再读取时就会抛出java.io.InvalidClassException异常。
异常原因
在整个推的项目中,消息是一个Message类,Message必须转换为字节传输,因此在Message类中实现了Serializable接口,在服务器中将Message编码传输到客户端中解码,就是这个环节发生了错误,这是因为服务端的Message类和客户端的Message类的包路径不一样,导致编码和解码不一致,自然会报异常!
解决方案
可以在序列化对象类中的成员属性中加上serialVersionUID(取值随意),然后重新执行写入和读取操作即可
public class User implements Serializable {
private final long serialVersionUID = -1L;
private int id;
private String userName;
private String password;
private double money = 0;
private int count = 3;
public User() {
}
public User(int id, String userName, String password) {
this.id = id;
this.userName = userName;
this.password = password;
this.money = money;
this.count = count;
}
```get&set 略
}