java.io.InvalidClassException异常解决方案

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 略
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值