This is the dataoutputstream method ~~ UTF-8 encoding is actually changed from Unicode. utf8 encoding converts the ASC encoding into one byte and the other two to three bytes! Because datainput (output) stream is a byte stream, other formats can be converted using this encoding.
- Writeutf (string Str );
- Write (IntB );
- Writebytes (string S );
- The parameters that can be passed in these methods are different.
- Both writeutf and writebytes can pass string-type parameters, but write won't work.
- Public Final VoidWriteutf (string Str)
- ThrowsIoexception writes a string to the base output stream using the UTF-8 modified encoding in a machine-independent manner.
- First, write two bytes to the output stream through the writeshort method, indicating the number of bytes followed. This value is the actual number of bytes written, not the length of the string. Based on this length, each character of the string is output in sequence using the character's UTF-8 modified encoding. If no exception is thrown, the counter written increases the total number of bytes written to the output stream. The value must be at least 2 plus STR, and at most three times the length of 2 plus Str.
- String readutf ()
- ThrowsIoexception reads a string encoded in a modified format using the UTF-8. The standard protocol for readutf is that this method reads the representation of a unicode string encoded using a modified UTF-8 format, and returns this string as a string.
- First, read two bytes and use them to construct an unsigned 16-bit integer. The construction method is identical to that of the readunsignedshort method. This integer is called the UTF length, which specifies the number of additional bytes to be read. Then convert these bytes into characters in groups. The length of each group is calculated based on the value of the first byte in the group. The byte that follows a group (if any) is the first byte in the next group.
- If the first byte in the group matches the bitwise Mode 0 xxxxxxx (where X indicates "may be 0 or 1"), the group only has this byte. This byte is left-side and converted into a single character.
- If the first byte in the group matches the bit mode 110 XXXXX, the group only consists of byte A and another byte B. If there is no byte B (because byte A is the last byte to be read), or if byte B does not match the bit mode 10 xxxxxx, A utfdataformatexception is thrown. Otherwise, convert the group into characters:
- (Char) (A & 0x1f) <6) | (B & 0x3f ))
- If the first byte in the group matches the bit mode 1110 XXXX, the group consists of byte A and the other two byte B and C. If there is no byte C (because byte A is one of the last two bytes to be read), or if byte B or byte C does not match the bit mode 10 xxxxxx, utfdataformatexception is thrown. Otherwise, convert the group into characters:
- (Char) (A & 0x0f) <12) | (B & 0x3f) <6) | (C & 0x3f ))
- If the first byte in the group matches the pattern 1111 xxxx or the pattern 10 xxxxxx, utfdataformatexception is thrown.
- If any time during execution reaches the end of the file, an eofexception is thrown.
- After each group is converted into characters through this process, the corresponding group is read from the input stream and these characters are collected together to form a string. Then the string is returned.
- You can use the writeutf method of the dataoutput interface to write data suitable for this method.