问题描述
DataOutputStream
中的 writeBytes(str)
与 write(str)
有什么区别?并有使用它们的提示/技巧吗?预先感谢.
What's the difference between writeBytes(str)
vs write(str)
in DataOutputStream
?And is there any Tips/Tricks to use them?Thanks in advance.
推荐答案
DataOutputStream属于用于写入二进制数据的OutputStream类-不是用于文本的Writer,它是旧类,并且是 writeBytes(String)
是一个奇怪的twitter方法:
DataOutputStream belongs to the OutputStream classes for writing binary data - not Writer for text, It is an old class and writeBytes(String)
is a weird twitter method as it:
因此,从每个Unicode UTF-16字符(16位)中提取低字节.如果字符串限制为7位ASCII(可能是ISO-8859-1),则该字符串不整齐.但总的来说,信息会丢失.
So from every Unicode UTF-16 char (16 bits) the low byte is taken. If the string restricted to 7-bits ASCII, maybe a bit ISO-8859-1, the string is not mangled. But in general information is lost.
DataInputStream中没有对应项,没有 String readBytes()
.
There is no counterpart in DataInputStream, no String readBytes()
.
我称它为设计事故,因为Java引入了文本和二进制数据( byte []
)的分离,引入了 byte
并保留了 String
和16位 char
(用于Unicode文本).作者可能觉得需要C风格的 write(char *)
.
I would call it a design mishap, as java introduced a separatation from text and binary data (byte[]
), introducing byte
and reserving String
and 16-bit char
for Unicode text. The author probably felt a need for a C style write(char*)
.
无需提及writeUTF和DataInputStream.readUTF.
这篇关于DataOutputStream中的writeBytes(str)与write(str)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!