Java Serializable Object to Byte Array

后端 未结 12 1320
春和景丽
春和景丽 2020-11-22 03:12

Let\'s say I have a serializable class AppMessage.

I would like to transmit it as byte[] over sockets to another machine where it is rebuil

12条回答
  •  攒了一身酷
    2020-11-22 03:30

    Can be done by SerializationUtils, by serialize & deserialize method by ApacheUtils to convert object to byte[] and vice-versa , as stated in @uris answer.

    To convert an object to byte[] by serializing:

    byte[] data = SerializationUtils.serialize(object);
    

    To convert byte[] to object by deserializing::

    Object object = (Object) SerializationUtils.deserialize(byte[] data)
    

    Click on the link to Download org-apache-commons-lang.jar

    Integrate .jar file by clicking:

    FileName -> Open Medule Settings -> Select your module -> Dependencies -> Add Jar file and you are done.

    Hope this helps.

提交回复
热议问题