What's the difference between writeUTF and writeChars?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-21 04:37:11

问题


What's the difference between writeUTF and writeChars? (methods of ObjectOutputStream) Further I have not found the corresponding readChars in ObjectInputStream.


回答1:


writeUTF writes text in UTF-8 format encoding preceeded with text length, so readUTF knows how many characters to read from stream.

writeChars writes text as a sequence of 2-bytes chars with no length. To read it, we should use readChar method and we need to know how many chars were written.




回答2:


writeChars() uses Unicode values

Writes every character in the string s, to the output stream, in order, two bytes per character. If s is null, a NullPointerException is thrown. If s.length is zero, then no characters are written. Otherwise, the character s[0] is written first, then s1, and so on; the last character written is s[s.length-1]. For each character, two bytes are actually written, high-order byte first, in exactly the manner of the writeChar method.

writeUTF() uses a slightly-modified version of UTF-8

Writes two bytes of length information to the output stream, followed by the modified UTF-8 representation of every character in the string s. If s is null, a NullPointerException is thrown. Each character in the string s is converted to a group of one, two, or three bytes, depending on the value of the character.



来源:https://stackoverflow.com/questions/18945202/whats-the-difference-between-writeutf-and-writechars

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!