简体   繁体   English

DataOutputStream中的writeBytes(str)与write(str)

[英]writeBytes(str) vs write(str) in DataOutputStream

What's the difference between writeBytes(str) vs write(str) in DataOutputStream ? DataOutputStream writeBytes(str)write(str)什么区别? And is there any Tips/Tricks to use them? 并且有使用它们的提示/技巧吗? Thanks in advance. 提前致谢。

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: DataOutputStream属于用于写入二进制数据的OutputStream类,而不是用于文本的Writer,它是一个旧类,而writeBytes(String)是一个怪异的twitter方法,因为它是:

Each character in the string is written out, in sequence, by discarding its high eight bits. 字符串中的每个字符通过丢弃高八位来依次写出。 If no exception is thrown, the counter written is incremented by the length of s. 如果未引发异常,则写入的计数器将增加s的长度。

So from every Unicode UTF-16 char (16 bits) the low byte is taken. 因此,从每个Unicode UTF-16字符(16位)中提取低字节。 If the string restricted to 7-bits ASCII, maybe a bit ISO-8859-1, the string is not mangled. 如果字符串限制为7位ASCII(可能是ISO-8859-1),则不对字符串进行处理。 But in general information is lost. 但是总的来说,信息会丢失。

There is no counterpart in DataInputStream, no String readBytes() . 在DataInputStream中没有对应项,没有String readBytes()

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. 我称它为设计事故,因为Java引入了文本和二进制数据( byte[] )的分离,引入了byte并为Unicode文本保留了String和16位char The author probably felt a need for a C style write(char*) . 作者可能觉得需要C风格的write(char*)

No need to mention writeUTF and DataInputStream.readUTF. 无需提及writeUTF和DataInputStream.readUTF。

write() writes a byte[] to the stream, whereas writeBytes() writes the output of yourString.getBytes() to the stream. write()byte[]写入流,而writeBytes()yourString.getBytes()的输出写入流。 If we needed a string from the byte[] we could String yourString = new String(yourByteArray); 如果我们需要byte[]的字符串,则可以使用String yourString = new String(yourByteArray);

As you can see with a String it matters little which approach we use, we can convert to the correct object. 如您所见,使用String无关紧要,我们可以使用哪种方法转换为正确的对象。

However what if you wanted to send binary data? 但是,如果要发送二进制数据怎么办? You probably have a byte[] or a ByteArrayOutputStream , that you can use to write to your data stream directly. 您可能有一个byte[]ByteArrayOutputStream ,可用来直接写入数据流。

In DataOutputStream , in reference to the oracle documentation ( http://docs.oracle.com/javase/7/docs/api/java/io/DataOutputStream.html ) there isn't any method named write(String) but only write(byte[]) , write(byte[] b, int off, int len) and write(int b) . DataOutputStream ,参考oracle文档http://docs.oracle.com/javase/7/docs/api/java/io/DataOutputStream.html )时,没有任何名为write(String) ,只能write(byte[])write(byte[] b, int off, int len)write(int b) So, if you have a String , the simplest method you can use is writeBytes(String) . 因此,如果您具有String ,则可以使用的最简单方法是writeBytes(String)

There is no difference between those methods, only use the proper one according to your needs (the type of the object your data are stored in). 这些方法之间没有区别,仅根据需要使用适当的方法(数据存储在其中的对象的类型)。

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

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