我有这个发送字符串或整数,但如果我想发送一个字符串数组我应该使用什么?提前如何使用套接字和objectoutputstream发送字符串数组
// A string ("Hello, World").
out.write("Hello, World".getBytes());
// An integer (123).
out.write(ByteBuffer.allocate(4).putInt(123).array());
感谢
我有这个发送字符串或整数,但如果我想发送一个字符串数组我应该使用什么?提前如何使用套接字和objectoutputstream发送字符串数组
// A string ("Hello, World").
out.write("Hello, World".getBytes());
// An integer (123).
out.write(ByteBuffer.allocate(4).putInt(123).array());
感谢
如果您使用ObjectOutputStream
只写阵列
ObjectOutputStream out = ...
String[] array = ...
out.writeObject(array);
,就没有必要使用字节数组渣土约 - 类提供了高层次的方法读取和写入整个对象。
同理:
out.writeInt(123);
out.writeObject("Hello, World");
你只需要如果你使用的原料,低层次的OutputStream
类使用write(byte[])
方法。
我强烈建议使用'.getBytes(“UTF-8”)'为您的字符串获取安全的字节编码。 –