Often listen to everyone talk about "Java using Unicode encoding", I do not know they really know, but also the same!
To begin with, UTF-8 encoding is used in the class file in Java, and utf-16 (such as char) is used when the JVM is running.
Write and read files in the form of a byte
// FileOutputStream out in the form of a byte stream new FileOutputStream ("Text.txt" ); Out.write ( "Iamchinese". GetBytes ("Utf-8" )); Out.write ( I am Chinese). GetBytes ("Utf-8" )); Out.close (); // read in the form of a byte stream FileInputStream in = new FileInputStream ("Text.txt" ); byte [] array = new byte [25 new String (Array, "Utf-8"
Whether it's "Iamchinese" or "I'm Chinese", for the time being, regardless of what byte it uses in Java for Unicode encoding, we just have to focus on the logic (where they are all the same literal) and they are String , and then we want to save the utf-8 encoded byte stream of these strings to the file, So when reading we can follow the saved encoding format and then get the string literal value (because the English character uses utf-8 encoding to occupy a byte, while a Chinese encoding takes 3 bytes, all of which use a byte array of length 25 to hold bytes read from the file). When you get here, do you think that the new newly-created string it internally holds the characters are utf-8 format, the fact is not, has been emphasized, Java is used in Unicode encoding, so at the time of new, The method internally silently makes the stored Unicode characters.
Second, the character of the form of writing, read files
//write in the form of a character streamBufferedWriter out =NewBufferedWriter (NewOutputStreamWriter (NewFileOutputStream ("Text.txt"), "Utf-8")); Out.write ("Iamchinese"); Out.write ("I am a Chinese"); Out.close (); //Read one by one in the form of a character streamBufferedReader in =NewBufferedReader (NewInputStreamReader (NewFileInputStream ("Text.txt"), "Utf-8")); intC; inti = 0; while((c = In.read ())! = 1) {System.out.println (Char) c); System.out.println (c); I++; } System.out.println ("A total of" + i + "characters"); In.close ();
The code above writes the data in the same way as the byte stream, except that it uses the API to manipulate it directly, omitting the process of constructing bytes in one step . But the strange thing is that In.read () This code (API Doc note indicates that it reads a character, that is, whether your character stream is utf-8 or GBK encoded, it is definitely returning a character), it is strange that its return value is a 0-65535 integer, It's going to get me so bad, how could it be? If the file uses GBK encoding, then the 2 bytes (0-65535) used to read it can be saved, but when using Utf-8 a Chinese three bytes Ah, how is it converted??!! In fact, this is not the case when the read returned by the integer is just this character corresponding to the Unicode encoding when the double-byte representation of the integer, not the character is stored when the specific encoding corresponding to the byte and then converted to an integer. This also shows that in the inside of read it has converted our specified encoding format characters into Unicode encoding, and when reading a different encoded text file, as long as we correctly specify the encoding scheme, then the Read method will fetch itself to read a character ( Change the file to three bytes (Utf-8 in Chinese) or a byte (Utf-8 's English letter) or two characters (GBK encoded in Chinese)), and then translate the character from the Unicode code table to a 2-byte integer.
C. Encoding Chinese characters in Java
Char c= ' in '; System.out.println ((int) c); int a=20013; System.out.println ((char) a); Char cc= ' a '; System.out.println ((int) cc); System.out.println (Arrays.tostring ("a". GetBytes ("Utf-8"));
Iv. Matters of note
1. No matter what the format of the file: byte stream (jpg, RMVB) or character formatting (txt), when they are saved to disk is the form of bytes, when the various applications read (regardless of the use of character stream, byte stream) is a byte of a byte of reading, just a variety of flow API after using decorative mode, There may be buffering, the encapsulation of characters (which will be used when the file is saved using the encoding format, such as Utf-8 or GBK) operation, the direct call API provided to the user by its name "words too literally", thus having a character stream.
2. All files can be read in the form of a byte stream. However, for character files, if you simply use a byte stream to read (and also need to decode the character text), then you must know in advance that the character file is used in the encoding format, no, we get only bytes, when using an array of bytes to convert characters, strings (such as new string (bytes, "Utf-8");) error (and the actual encoding character encoding using GBK), which is the essence of the appearance of garbled.
3. Since all the files can be read in the form of a byte stream, why is there a character stream? Providing a character stream in the form of reading is just for our convenience to read characters to the file, providing us with direct read characters of the API (such as ReadLine (), rather than each time the user reads the bytes themselves, allowing the characters to be generated after themselves according to the encoded format.
4. What is Java text file encoding format, class file encoding format, JVM runtime encoding format? What is the difference between them and their effects? The first two are the character set types that are used when a file is saved to disk, regardless of the song format, as long as the same character set is used when reading and writing. The last accesses than either is in memory to represent the byte form of the character.
Any character in 5.java is two bytes, so a character such as the English letter is one byte in Utf-8, but in Unicode it is a waste of one byte of storage.
6. As for the file in Utf-8, how is the character stream distinguished because it uses different bytes for different types of characters during encoding? You can refer to the UTF-8 Coding specification, which can naturally be known.
Characters and Bytes in Java