简体   繁体   English

如何使用DataInputStream的方法读取DataOutputStream.writeBytes(String)写入的数据?

[英]How to use the method of DataInputStream to read data writed by DataOutputStream.writeBytes(String)?

How to use the method of DataInputStream to read data writed by DataOutputStream.writeBytes(String)? 如何使用DataInputStream的方法读取DataOutputStream.writeBytes(String)写入的数据?

I really have no idea of this problem! 我真的不知道这个问题!

writeBytes(String) does not include any indication of the length written so unless you have output the length earlier there is no way to know how much to read. writeBytes(String)不包括写入长度的任何指示,因此,除非您提前输出了长度,否则无法知道要读取多少。

Also note that writeBytes(String) only writes the bottom eight bits of each character in the string discarding the high bits - this may not be what you want. 另请注意, writeBytes(String)仅将writeBytes(String)中每个字符的低八位写入高位,而这可能不是您想要的。

If you want to write out a String look at writeUTF which does include the string length and can be read with DataInputStream.readUTF . 如果要写出String查看writeUTF ,它确实包含字符串长度,并且可以使用DataInputStream.readUTF读取。

You can construct your DataOutputStream with an ByteArrayOutputStream and your DataInputStream with an ByteArrayInputStream using as buffer the byte array from ByteArrayOutputStream , for instance: 你可以建立你的DataOutputStreamByteArrayOutputStream和您的DataInputStreamByteArrayInputStream使用作为缓冲来自字节数组ByteArrayOutputStream ,例如:

ByteArrayOutputStream inMemory = new ByteArrayOutputStream ();
DataOutputStream out = new DataOutputStream (inMemory);

DataInputStream in = new DataInputStream (new ByteArrayInputStream  (inMemory.toByteArray()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM