简体   繁体   English

在JAVA中可序列化的ObjectOutputStream / Input

[英]ObjectOutputStream/Input Serializable in JAVA

i am working with ObjectOutputStream to save my object to a .dat file. 我正在与ObjectOutputStream一起将对象保存到.dat文件。 My problem is, that if i CHANGE source code of my object (for example i add ONE method(getter)) the input stream cannot load data and tell me a error about Serializable : It is possible to solve this problem? 我的问题是,如果我更改对象的源代码(例如,我添加一个method(getter)),则输入流无法加载数据并告诉我有关Serializable的错误:可以解决此问题吗? I must generate new .dat file everytime after if i change my source code. 如果更改源代码,则每次之后都必须生成新的.dat文件。

Using this method: ( DONT LOOK AT THE OBJECT TYPE-return value ) SAVE 使用此方法:( 不要查看对象类型的返回值 )保存

public void saveToFile(HeaderOfMapTeachStructure hm, String nameOfFile) {
    try (ObjectOutputStream os = new ObjectOutputStream(
            new FileOutputStream(nameOfFile + "." + this.TYPE_OF_FILE))) {
        os.writeObject(hm);
    } catch (IOException e) {
        System.out.println("Error: " + e);
    }
}

LOAD 加载

public MapStandard loadFromFileMS(String nameOfFile) {
    MapStandard hm = null;
    InputStream inputStreaminputStream
            = getClass().getClassLoader().
            getResourceAsStream("data/" + nameOfFile + ".data");
    try {
        try (ObjectInputStream is = new ObjectInputStream(inputStreaminputStream)) {
            hm = (MapStandard) is.readObject();
        }
    } catch (IOException | ClassNotFoundException e) {
        System.out.println("Error: " + e);
    }
    return hm;
}

ERROR IS: 错误是:

Error: java.io.InvalidClassException: MapVerb.RealVerb; local class incompatible: stream classdesc serialVersionUID = -887510662644221872, local class serialVersionUID = 7992494764692933500

It is possible to solve this problem? 有可能解决这个问题吗?

Yes. 是。 The reason the serialization breaks is because the generated version number changes. 序列化中断的原因是因为生成的版本号更改。 If you specify this explicitly, it won't be generated, and you'll be in control of versioning. 如果您明确指定此选项,则不会生成它,并且您将控制版本控制。 That means you need to be careful though - if you change the fields which are in the object, or the meaning of those fields, you'll need to remember to change the version number yourself. 但这意味着您需要小心-如果更改对象中的字段或这些字段的含义 ,则需要记住自己更改版本号。

You specify the serialization version like this: 您可以这样指定序列化版本:

// It doesn't have to be constant, but that's fairly common.
private static final long serialVersionUID = ...; // Some constant

See the documentation for Serializable for more information. 有关更多信息,请参见可Serializable的文档。

You might also want to consider using alternative approaches for storing data - the default binary serialization in Java is fairly brittle. 您可能还需要考虑使用其他方法来存储数据-Java中的默认二进制序列化非常脆弱。 There are lots of alternatives - text-based formats such as XML, JSON or YAML, or binary formats such as Protocol Buffers or Thrift. 有很多选择-基于文本的格式(如XML,JSON或YAML)或二进制格式(如协议缓冲区或Thrift)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM