How to send a string array using sockets and objectoutputstream

百般思念 提交于 2019-12-11 03:38:14

问题


I have this to send string or integer but if i want to send a string array what should i use?

  // A string ("Hello, World").
    out.write("Hello, World".getBytes());

    // An integer (123).
    out.write(ByteBuffer.allocate(4).putInt(123).array());

Thanks in advance


回答1:


Just write the array

ObjectOutputStream out = ...
String[] array = ...     
out.writeObject(array);

If you're using ObjectOutputStream, there's no need to muck about with byte arrays - the class provides high-level methods to read and write entire objects.

Similarly:

out.writeInt(123);
out.writeObject("Hello, World");

You only need to use the write(byte[]) methods if you're using the raw, low-level OutputStream class.



来源:https://stackoverflow.com/questions/7127227/how-to-send-a-string-array-using-sockets-and-objectoutputstream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!