Objective
Character encoding, this is not part of the IO content, but the byte stream after the write should be a character stream, since it is a character stream, that involves a "character encoding" problem, considering that the character encoding is not only in the IO this block, Java many scenes are involved in this concept, so this article is specifically written about the character encoding, specific There are many online, but the purpose of this article is to make it clear the role of various coding methods, personally, do not ask, there is no need to understand the character coding in depth.
Character Set and character encoding
The first concept is the difference between a character set and a character encoding:
1. Character Set (charset)
A character set refers to a collection of all the abstract characters supported by a system. Characters are the general name of all kinds of words and symbols, including the national text, punctuation, graphic symbols, numbers, etc., the common character set has the ASCII character set, the GB2312 character set, the BIG5 character set, the GB18030 character set, the Unicode character set and so on.
2, character encoding (encoding)
In order to accurately handle various character sets, the computer will encode characters so that the computer can recognize and store various text. So character encoding is the number of digital systems that a computer can accept as a symbol, called a digital code.
ASCII code
The computer is only the number 0 and 1 (strictly speaking, even 0 and 1 are not, only open and close, nothing more than 0 and 1 means the state of the switch), in the computer software is the amount of digital identification, the screen shows a character is also a number. The first computers used in the United States have very few characters, so each character is represented by a number, and a byte can represent a number that is internal security enough to hold all these characters. In fact, the number of bytes of these characters is the highest bit of the byte is 0, that is, these numbers are between 0~127, such as the character a corresponds to 97, the character B corresponds to the number 98, the character and the corresponding encoding of the number is fixed, this set of coding rules is called ASCII code (US standard Information Interchange Code). A simple ASCII code table
You can see from the table that the ASCII code is divided into two parts:
1, 0~31 is control characters, such as line break, enter, delete, etc.
2, 32~126 is printed characters, can be entered by the keyboard and can be displayed
GB2312 and GBK
With the popularization of computers in other countries, many countries have introduced the local character set into the computer, greatly expanding the range of characters in the computer. A byte can represent a range that is not large enough to hold Chinese characters (see the ASCII Code table above), and mainland China will use two bytes for each medium character, and the encoding of the original ASCII code remains unchanged.
In order to distinguish between a Chinese character and two ASCII characters, the highest bit of each byte of Chinese characters is 1, and China has specified a corresponding number for each medium character, and in 1980 set up a set of "Information technology encoding Character set", this set of specifications is GB2312 . GB2312 is a double-byte encoding, the total encoding range is a1~f7, in fact, A1~a9 is the Regal district, a total of 682 symbols; B0~f7 is a Chinese character area with a total of 6,763 characters.
GBK is a follow-up standard established in 1995 , the full name of "Chinese character Code extension Code", is the National Technical Supervision Bureau for Windows 95, the new Chinese character code specification. The advent of GBK is to expand GBK2312 and add more kanji. GBK encoding range is 8140~fefe (remove xx7f), a total of 23,940 code points, can represent 21,003 Chinese characters, its encoding is and GB2312 compatible, that is, GB2312 encoded Chinese characters can be decoded with GBK, and there will be no garbled problem. GBK is also the system default encoding for Chinese Windows operating systems today.
Unicode
A character that appears in a country's localization system, which is sent by e-mail to another country's localization system, is not the original character, but the other country's character or garbled, because there are no real characters in the computer, characters are in the form of numbers, sent by mail A character that actually transmits the character encoding corresponding to this character, the same number represented in different countries and regions is likely to be different symbols.
In order to solve the inconvenience of using different localized character encodings for each country and region, people all the symbols in the world are uniformly encoded, called Unicode (Uniform Code, universal Code). All characters no longer distinguish between countries and regions, are all human symbols, such as "medium" in Unicode is no longer GBK in the d6d0, but everywhere is 4e2d, if all computer systems use this encoding, then 4e2d the word in any place represents the "medium" of Chinese characters. Unicode-encoded characters occupy a size of two bytes, meaning that no more than 65,536 characters are in the world.
Of course, Unicode contains only 65,536 characters to want to contain all the characters in the world is not enough, so Unicode provides a word ideographic surface mapping, the link address is the wiki Wikipedia for the ideographic polygon mapping interpretation. Another thing to mention is that Unicode is the basis for Java and XML.
UTF-8 and UTF-16
Unicode is a character set standard, and how it should be applied to a computer is another topic, and there are two common ways of encoding Unicode:
1, UTF-16. A two-byte representation of the Unicode conversion format, which is a fixed-length representation. That is to say, no matter what character can use two bytes, two bytes is 16Bit, so called UTF-16. UTF-16 encoding is very convenient, each two bytes represents a character, which greatly simplifies the operation of the string operation.
2, UTF-8. UTF-16 Unified uses two bytes to represent a character, although it is very simple to represent, but a large part of the character is enough to represent a byte, now requires two bytes, storage space is magnified by one. UTF-8 takes a variable-length technique where each coded area has a different loadline length, and different types of characters can be composed of 1~6 bytes.
Comparing the two coding methods, the UTF-16 is relatively efficient, the conversion from character to byte can be simpler, the string operation is better, it is more suitable to use between the local disk and memory, can be a fast switching between characters and bytes. However, UTF-16 is not suitable for transmission between the network, because the network transmission is prone to damage the byte stream, once the byte stream corruption will be difficult to recover, so compared to UTF-8 more suitable for network transmission. In addition UTF-8 to ASCII characters with single-byte storage, the single character damage will not affect the subsequent characters, in the coding efficiency between GBK and UTF-16, so, UTF-8 in the coding efficiency and coding security balance, is the ideal Chinese encoding method.
Java and character encoding
characters in Java use Unicode character sets, encoded in UTF-16, and Java technology supports a fully expanded native platform character set with Unicode guarantees across platform features, while both display output and keyboard input are local encodings. Therefore, the transformation of the two problems inevitably.
Look at a very simple example:
Throws exception{ //byte b[] = "Let's learn the Java language together". GetBytes ("GB2312"new File ("D:/files/encoding.txt "new FileOutputStream (file); Out.write (b); Out.close ();}
Take a look at what's in the file:
Normal output, no coding issues, but if so:
Throws exception{ //byte b[] = "Let's learn the Java language together". GetBytes ("iso8859-1"new File ("d:/files/ Encoding.txt "new FileOutputStream (file); Out.write (b); Out.close ();}
And look at what's in the file:
Garbled problem appears, through the above operation of the complete process to analyze the reasons.
To illustrate again,string in Java is in the Unicode character set . Each class in Java is well-supported for English characters and can be written to the file normally, but not for Chinese characters. From the Java source code to the correct content of the input file, to go through the "Java source code->java bytecode----" virtual machine--file "steps, each step in the above process must correctly handle the encoding of Chinese characters, in order to be able to finally have the results we expect.
"Java source code->java bytecode", the standard Java compiler Javac uses the character set is the system default character set, for example, in the Chinese Windows operating system is GBK (above the GBK part has been explained), and on the Linux operating system is Iso8859-1, so you will find that the Linux operating system compiled on the source file in the text characters are problematic, the solution is to add the encoding parameters when compiling, so as to be independent of the platform, the use is: javac-encoding GBK.
Java bytecode, virtual machine-and file, the Java Runtime Environment (JRE) is available in English and international versions, but only the international version supports non-English characters. The Java Development Kit (JDK) certainly supports multi-country characters, but not all computer users have the JDK installed. Many operating system applications are embedded with the international version of the JRE in order to better support Java, providing the convenience of supporting their own multi-national characters.
The problem is "Java source code->java bytecode", which is caused by the JDK setting environment variable. Use the program to look at the JDK environment variables:
void Main (string[] args) { system.getproperties (). List (System.out);}
Look at the output of all the information, a bit long:
1--Listing properties--2 java.runtime.name=Java (TM) SE Runtime Environment3 sun.boot.library.path=E:\MyEclipse10\Common\binary\com.sun ....4 java.vm.version=11.3-B025 java.vm.vendor=Sun Microsystems INC.6 Java.vendor.url=http://java.sun.com/7 path.separator=;8 Java.vm.name=java HotSpot (TM) 64-Bit Server VM9 file.encoding.pkg=Sun.ioTen user.country=CNSun.java.launcher=Sun_standardSun.os.patch.level=Java.vm.specification.name=Java Virtual Machine SpecificationUser.dir=F:\ Code \myeclipse\testiojava.runtime.version=1.6.0_13-B03java.awt.graphicsenv=Sun.awt.Win32GraphicsEnvironmentjava.endorsed.dirs=E:\MyEclipse10\Common\binary\com.sun ....Os.arch=Amd64Java.io.tmpdir=C:\Users\dell1\AppData\Local\TempLine.separator=21stJava.vm.specification.vendor=Sun Microsystems INC.User.variant=Os.name=Windows Vistasun.jnu.encoding=GBKJava.library.path=E:\MyEclipse10\Common\binary\com.sun ....Java.specification.name=Java Platform API Specificationjava.class.version=50.064 Sun.management.compiler=hotspot-Bit Server Compileros.version=6.2User.home=C:\Users\dell1User.timezone=java.awt.printerjob=Sun.awt.windows.WPrinterJobfile.encoding=GBKjava.specification.version=1.6Approx. user.name=Dell1Panax Notoginseng java.Class.path=F:\ Code \myeclipse\testio\binjava.vm.specification.version=1.0Sun.arch.data.model=64Java.home=E:\MyEclipse10\Common\binary\com.sun ....Java.specification.vendor=Sun Microsystems INC.User.language=Zhawt.toolkit=Sun.awt.windows.WToolkitjava.vm.info=Mixed modejava.version=1.6.0_13java.ext.dirs=e:\myeclipse10\common\binary\com.sun .... 47 sun.boot. class.path=e:\myeclipse10\common\binary\com.sun .... 48 java.vendor=sun Microsystems inc.49 file.separator=50 Java.vendor.url.bug=http://java.sun.com/cgi-bin/bugreport ... 51 sun.cpu.endian=little 52 sun.io.unicode.encoding=unicodelittle53 sun.desktop=windows54 sun.cpu.isalist=amd64
Note that the 34 lines indicate that the JDK uses the GBK character set (GBK is an extension on the GB2312, so there is certainly no problem with the GB2312 character set), which means that Java's operation of string has been converted from Unicode to GBK. Since the JDK uses the GBK encoding, it is of course problematic to use the iso8859-1 character set to display GBK encoded in Chinese.
This is only an example, in our application involving I/O operations, generally as long as the attention of the specified unified codec charset set, there will be no garbled problem. For some applications that do not pay attention to the specified character encoding, the operating system's default encoding is used in the Chinese environment. If the codec is in the Chinese environment, there is usually no problem, but it is strongly recommended not to use the operating system's default encoding, because this will make your application's encoding format and run-time environment binding, so the cross-environment is likely to have garbled problems.
Java IO4: Character encoding