简体   繁体   English

从文件加载(不兼容的类)

[英]Load from file (incompatible classes)

This is my first post to Stackoverflow, please be gentle :) I'm a beginner java developer working on an application for work where a class state needs to be saved to the network drive. 这是我对Stackoverflow的第一篇文章,请客气:)我是一名初学者Java开发人员,致力于将类状态保存到网络驱动器的应用程序。 I found it the easiest to save separately the attributes(there are only 2), as one attribute is accessed and modified(saved and loaded from) in a file a lot more than the other. 我发现最简单的方法是分别保存属性(只有2个),因为在文件中访问和修改(保存和加载)一个属性比保存另一个属性要多得多。 On the other hand I would like to work on the program and improve it while it's in heavy use. 另一方面,我想在程序大量使用的同时对其进行改进。 The problem is that if I change something in the class implementation, then it's not compatible anymore with the state loaded from the file with the previous version of the class. 问题是,如果我在类实现中更改了某些内容,那么它就不再与使用该类的先前版本从文件加载的状态兼容。

An example : Old Class: - name - address - phone number 示例:旧类:-名称-地址-电话号码

New class: - name - address - phone number - age 新课程:-名称-地址-电话号码-年龄

Loading the old class from a file into the new class results in exception thrown being the loaded class incompatible with the new one. 将旧类从文件加载到新类中会导致异常,因为加载的类与新类不兼容。 Is there any solution around it? 周围有什么解决办法吗?

Thank you for your help in advance! 提前谢谢你的帮助! :) :)

add: ohh, I'm using ObjectOutputStream for saving the file. 添加:哦,我正在使用ObjectOutputStream保存文件。

Store the state as XML. 将状态存储为XML。 There is JAXB that in the new version can purely with annotations store/load class objects to/from XML. 在新版本中有一个JAXB可以完全通过注释将类对象存储到XML中或从XML中加载类对象。

This leaves more control (= needs more discipline to code what to persist). 这使得更多的控制(=需要更多的纪律代码,坚持什么 )。 But is easy. 但是很容易。 Also can during development patch the XML a bit. 在开发过程中也可以对XML进行一点修补。 (You need not use full JAXB with XSD.) (您不需要将完整的JAXB与XSD一起使用。)

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

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