How to deserialize from a file to different class

前端 未结 2 827
無奈伤痛
無奈伤痛 2021-01-12 21:49

I serialize a ArrayList list to a file and transmitted the file to another system in another machine.

And since it\'s a differ

相关标签:
2条回答
  • 2021-01-12 22:36

    Yes, there are one of the three things you can do.

    1. Create the same class with the same package name

    2. Create an interface(ofcourse again in the same package on both the machines) and ship it on client machines. All your classes can implement this interface even in different packages. For example, your interface can be named Loggable and it should have all the methods declared which are in ChangeLog class right now.

    3. Or finally instead of using the class "ChangeLog" just completely avoid using it and use a HashMap instead which is serializable by default. And you will not have to ship anything to your client(other) machine. You will be able to serialize ArrayList and convert it back to object on other machine.

    All the Classes which implement class Loggable can then cast to that type. I hope this helps

    0 讨论(0)
  • 2021-01-12 22:45

    I don't have the same packageA.Changelog class, instead is a packageB.Changelog which has exactly same structure but in different package.

    Why? This is the problem. Don't do that.

    0 讨论(0)
提交回复
热议问题