0

运行程序时出现此错误:

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();
}
}
4

2 回答 2

1

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

该消息是说您使用对象流序列化的类具有版本 id -1986042743597353721,但您的类版本 id 是-2452322360924937818L

所以你有2个选择:

  • 获取序列号为 -1986042743597353721 的类,或者...
  • 编写一个自定义反序列化器,它可以理解旧格式并可以构造一个新对象。

是完整的文档。

于 2013-08-07T05:14:02.427 回答
0

可能您的文件“grocery.bin”中填充了从具有 serialVersionUID = -1986042743597353721 的 Food.class 创建的 Food 对象,并且您正尝试将其转换为具有 serialVersionUID = -2452322360924937818 的 Food.class

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

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

于 2013-08-07T05:20:22.830 回答