开发者

How to send a string array using sockets and objectoutputstream

开发者 https://www.devze.com 2023-03-29 15:33 出处:网络
I have this to send string or integer but if i want to send a string array what should i use?开发者_JS百科

I have this to send string or integer but if i want to send a string array what should i use? 开发者_JS百科

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

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

Thanks in advance


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消