Java Serializable Object to Byte Array

后端 未结 12 1311
春和景丽
春和景丽 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:37

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

    // When you connect
    ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
    // When you want to send it
    oos.writeObject(appMessage);
    

    where it is rebuilt from the bytes received.

    // When you connect
    ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
    // When you want to receive it
    AppMessage appMessage = (AppMessage)ois.readObject();
    

提交回复
热议问题