Java and Unicode

Source: Internet
Author: User

Java and Unicode: Java class files are UTF-8 encoded, And the JVM runtime uses utf16. Java strings are unicode encoded. In short, Java uses the unicode character set to make it easier to internationalize. Which character sets are supported by Java: Which character sets can be recognized and correctly processed by Java? Check the Charset class. The latest JDK supports 160 character sets. You can use the static method availableCharsets to obtain all Java-supported character sets. Java code assertEquals (160, Charset. availableCharsets (). size (); Set <String> charsetNames = Charset. availableCharsets (). keySet (); assertTrue (charsetNames. contains ("UTF-8"); assertTrue (charsetNames. contains ("UTF-16"); assertTrue (charsetNames. contains ("gb2312"); assertTrue (Charset. isSupported ("UTF-8"); when do I need to pay attention to Encoding Problems? 1. read data from external resources: This is related to the encoding method adopted by external resources. We need to use the character set used by external resources to read external data: java code InputStream is = new FileInputStream ("res/input2.data"); InputStreamReader streamReader = new InputStreamReader (is, "GB18030"); you can see here, we use GB18030 encoding to read external data. By checking the encoding of streamReader, we can prove that the Java code assertEquals ("GB18030", streamReader. getEncoding (); it is precisely because we have specified the correct encoding for external resources that can be correctly decoded only when it is converted to a char array (GB18030-> unicode ): java code char [] chars = new ch Ar [is. available ()]; streamReader. read (chars, 0, is. available (); but we often write code like the following: Java code InputStream is = new FileInputStream ("res/input2.data"); InputStreamReader streamReader = new InputStreamReader (is ); in this case, what encoding method does InputStreamReader use to read external resources? Unicode? No. At this time, the encoding method is the default JVM character set. This default character set is determined when the VM is started, usually based on the language environment and the charset of the underlying operating system. You can use the following method to obtain the default JVM Character Set: Java code Charset. defaultCharset (); why? Because we read data from external resources, and the encoding method of external resources is usually the same as the character set used by the operating system, this default method is understandable. Okay, so I created a file through my IDE Ideas and read data from this file using the JVM default encoding method, but the read data is garbled. Why? The reason is that the files created through Ideas are UTF-8 encoded. To get a default JVM-encoded file, create a txt file manually. 2. the following code converts a string to a byte array: Java code "string ". getBytes (); but have you noticed the encoding used for this conversion? In fact, the above Code is equivalent to the following: Java code "string ". getBytes (Charset. defaultCharset (); that is, it converts a string into a byte array based on the JVM's default encoding (rather than the unicode you may think. On the contrary, how do I create a string from a byte array? Java code new String ("string". getBytes (); similarly, This method uses the default Character Set of the platform to decode the specified array of bytes (here decoding refers to decoding from a character set to unicode ). String encoding: Java code new String (input. getBytes ("ISO-8859-1"), "GB18030") What does the above Code represent? Someone will say, "convert the input string from the ISO-8859-1 encoding method to the GB18030 encoding method ". If this statement is correct, how can we explain that the java strings we just mentioned adopt unicode encoding? This statement is not only inappropriate, but also a big mistake. Let's analyze it one by one. The fact is that we should use GB18030 encoding to read data and decode it into a string, but the result is encoded with a ISO-8859-1, resulting in a wrong string. To recover the data, you must first restore the string to the original byte array, and then use the correct encoding GB18030 to re-Decode it into a string (that is, convert the data encoded in GB18030 to a unicode string ). Note that strings are always unicode encoded. But the encoding conversion is not as simple as negative, here we can correctly convert back, because the ISO8859-1 is a single-byte encoding, so each byte is converted to String as is, that is, although this is an incorrect conversion, the encoding has not changed, so we still have the opportunity to convert the encoding back! Conclusion: when dealing with java coding, we need to clearly understand three concepts: Java adopts unicode encoding, and JVM platform default character sets and external resource encoding. Program example: [java] public static void main (String args []) {System. out. println ("default charset:" + Charset. defaultCharset (); String str = "abc hello"; // string with UTF-8 charset byte [] bytes = str. getBytes (Charset. forName ("UTF-8"); // convert to byte array with UTF-8 encode for (byte B: bytes) {System. out. print (B + "");} System. out. println (); try {String str1 = new String (bytes, "UTF-8"); // to UTF-8 st Ring String str2 = new String (bytes, "ISO-8859-1"); // to ISO-8859-1 string String str3 = new String (bytes, "GBK"); // to GBK string System. out. println (str1); // abc Hello System. out. println (str2); // abc ?????? System. out. println (str3); // abc has been passed into the System. out. println (); byte [] bytes2 = str2.getBytes (Charset. forName ("ISO-8859-1"); for (byte B: bytes2) {System. out. print (B + "");} System. out. println (); String str22 = new String (bytes2, "UTF-8"); System. out. println (str22); // abc Hello System. out. println (); byte [] bytes3 = str3.getBytes (Charset. forName ("GBK"); for (byte B: bytes3) {System. out. print (B + "");} System. out. println (); String str33 = new String (bytes3, "UTF-8"); System. out. println (str33); // abc Hello} catch (UnsupportedEncodingException e) {e. printStackTrace () ;}} running result: [html] www.2cto. comdefault charset: GBK 97 98 99-28-67-96-27-91-67 abc Hello abc ?????? Abc too far 97 98 99-28-67-96-27-91-67 abc Hello 97 98 99-28-67-96-27-91-67 abc hello

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.