invalidClassException:请帮助解决这些错误

时间:2013-08-07 05:07:52

标签: java polymorphism serializable

运行程序时出现此错误:

java.io.InvalidClassException: Food; local class incompatible: stream classdesc serialVersionUID = -1986042743597353721, local class serialVersionUID = -2452322360924937818
    at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:570)
    at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1599)
    at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1494)
    at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1599)
    at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1494)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1748)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1327)
    at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1683)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1321)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:349)
    at GroceryStore.readInventory(GroceryStore.java:16)
    at GroceryStore.main(GroceryStore.java:39)

我尝试了很多东西,但不确定我哪里出错了,我对此事不熟悉。

    import java.io.Serializable;

    public abstract class Food implements Serializable {
    String Type, Name;
    double Price;
    private static final long serialVersionUID = -2452322360924937818L;

    public Food(String Type, String Name, double Price){
            this.Type = Type;
        this.Name = Name;
        this.Price = Price;
    }
    public String getType(){
        return Type;
    }
    public String getName(){
        return Name;
    }
    public double getPrice(){
        return Price;
    }
    public String toString(){
        return (Type + " " + Name + " " + Price);
    }
    public abstract double getQuantity();
}



    import java.io.Serializable;

    public class FoodItem extends Food implements Serializable {

     private static final long serialVersionUID = -2452322360924937818L;
     private double quantity;
     int bestBeforeMonth;
     int bestBeforeDay;


     public FoodItem(String Type, String Name, double Price, double quantity, int        bestBeforeMonth, int bestBeforeDay) {
        super(Type, Name, Price);
        this.quantity = quantity;
        this.bestBeforeDay = bestBeforeDay;
        this.bestBeforeMonth = bestBeforeMonth;
     }


     @Override
     public double getQuantity() {
        return quantity;
     }
     public void removeQuantity(double quantity){
        this.quantity = this.quantity - quantity;
     }

    }


    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectInputStream.GetField;
    import java.io.ObjectOutputStream;


    public class GroceryStore {
     static FoodItem[] inventory = null; 
     public static void readInventory(){

    try {
        ObjectInputStream in = new ObjectInputStream(new FileInputStream("grocery.bin"));
        inventory[0] =  (FoodItem) in.readObject();



    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
     }

public static void main(String[] args){
    readInventory();
}
}

2 个答案:

答案 0 :(得分:1)

此消息是由于类的序列化版本与当前类之间的版本不匹配而产生的。

消息显示您使用对象流序列化的类的版本号为-1986042743597353721,但您的班级版本ID为-2452322360924937818L

所以你有两个选择:

  • 获取序列版本为-1986042743597353721的课程,或者......
  • 编写一个了解旧格式的自定义反序列化器 可以构建一个新对象。

Here是完整的文档。

答案 1 :(得分:0)

你的文件“grocery.bin”可能是用Food.class创建的Food对象填充的,其中serialVersionUID = -1986042743597353721,你试图用serialVersionUID = -2452322360924937818

将它转换为Food.class

首先,我会尝试将您实现的类的serialVersionUID更改为-1986042743597353721。

如果出现其他相关异常,则与序列化对象和您的类存在实际差异。在这种情况下,您应该找出创建了grocery.bin的人或者是什么,并克隆其Food.java类。