dri*_*zom 2 java sockets objectoutputstream
我有这个发送字符串或整数,但如果我想发送一个字符串数组我应该使用什么?
// A string ("Hello, World").
out.write("Hello, World".getBytes());
// An integer (123).
out.write(ByteBuffer.allocate(4).putInt(123).array());
Run Code Online (Sandbox Code Playgroud)
提前致谢
只需编写数组
ObjectOutputStream out = ...
String[] array = ...
out.writeObject(array);
Run Code Online (Sandbox Code Playgroud)
如果您正在使用ObjectOutputStream,则无需使用字节数组 - 该类提供了读取和写入整个对象的高级方法.
同理:
out.writeInt(123);
out.writeObject("Hello, World");
Run Code Online (Sandbox Code Playgroud)
write(byte[])如果您正在使用原始的低级OutputStream类,则只需使用这些方法.
| 归档时间: |
|
| 查看次数: |
5576 次 |
| 最近记录: |