Java program garbled Problem and Solution

Source: Internet
Author: User

Java program garbled Problem and Solution

Recently, when writing a Java program, I occasionally encounter text garbled characters or remote words cannot be displayed normally. After searching for information and passing several tests, I have completely solved the problem.
 
First of all, we need to explain the character set that we often use, there are ISO8859-1, gb2312, GBK, gb18030, Unicode. Here the ISO8859-1 character set contains only English characters and is stored in one byte. The gb2312, GBK, and gb18030 character sets contain Chinese characters, both of which are compatible with the ISO8859-1 character set. Their character storage format is extended, where gb18030 contains GBK, and GBK contains gb2312. Unicode contains the characters of all countries in the world, Unicode is divided into UTF-8, UTF-16 and UTF-32 three, UTF-8 is a variable length character set, it is compatible with ISO8859-1, that is, English characters using a byte encoding, other characters are encoded in 2 to 4 bytes. Most of the Chinese characters are encoded in 3 bytes, and a few remote characters are encoded in 4 bytes, the UTF-16 uses both 2-byte encoding, which is not compatible with the ISO8859-1, the English character also uses two bytes, The UTF-32 uses 4-byte encoding, is not compatible with the ISO8859-1, visible UTF-16 and UTF-32 are a waste of space.
 
The most fundamental cause of garbled characters is the use of the wrong character set to decode the byte stream or encode the given string with the wrong character set into the wrong byte stream, for example, the two Chinese characters "Chinese, if you use the ISO8859-1 character set to encode it as a byte stream, because this character set does not support Chinese, so it will make an error, the output result is 3f3f, the meaning is ??. For example, the GBK byte stream of the word "Chinese" is D6 D0 ce C4, but if we use incompatible character sets to decode, such as using ISO8859-1 or UTF-8, the subsequent string is garbled or another character.
 
There are Encoding Problems in the process from developing Java programs to running Java programs. To avoid garbled code, you must understand the encoding processing conditions at any time.
 
Source code: When writing Java source code, we must save the written text in the file. No matter what editor we use, there is a problem, the source code (including Chinese characters) is saved to a file using the character set. Most editors obtain the current default Character Set of the system through the system environment variables, the editor will use this character set to save the source code we wrote to the file. In general, the default Character Set of our Chinese Windows system is gb18030, the default Character Set of Aix English environment is ISO8859-1, the default Character Set of Aix Chinese environment is IBM-euccn.
 
Compile: in the compilation. if the default processing is used for java files, javac will use the system's current default character set to read the source file, convert the content of the source file to UTF-8 encoding, and then compile, in this case, we can also use the-encoding parameter to specify a character set so that javac can use the character set we specified to read the source code and compile it. All the Chinese characters in the class file generated after compilation are encoded using the character set of the UTF-8, which is why the Java program can process any country text.
 
Runtime: When a Java program is running, it must use a Chinese character string defined inside the program, or it may use a Chinese character string read from the outside. After processing, these strings may be output to the outside of the program, encoding conversion is involved in these procedures, and the strings defined inside the program are stored in UTF-8. What character sets are used for processing the output from external reading and output to external program? If we do not specify a character set in the program, JVM determines which character set to use based on the system attribute. The name of this system attribute is file. encoding, we can set this value through the-D parameter when starting the Java program. If this value is not set, the JVM will determine this system Attribute Based on the system environment variable, in general, the default Character Set of our Chinese Windows system is gb18030, the default Character Set of Aix English environment is ISO8859-1, the default Character Set of Aix Chinese environment is IBM-euccn. In this way, the JVM will decode the byte stream according to this parameter when processing the input data, and then convert it to the UTF-8 format for internal processing in the Java program, then, encode the processed data according to this parameter and output it to the outside of the program. This is the usage of the character set when Java is running.
 
Now there is a problem. We usually develop in a Windows Chinese environment, and then run it on the AIX system. The default language environment of the AIX system is an English environment, so garbled characters will occur, the analysis process is as follows: the source file encoding format is gb18030, the default compilation, also use gb18030 to read the source file, Normal conversion to the UTF-8, generate class file, no special settings during running, the language environment for English environment, the default encoding for the ISO8859-1, so that when the output of Chinese will be normal UTF-8 representation of Chinese characters with the character set of the ISO8859-1 to encode the generation of byte stream, because the ISO8859-1 does not support Chinese characters, the result output is '? '. But this time but found that the external input to the Java program Chinese characters, but can be normal output, this is why, in fact, this is also the runtime default Character Set ISO8859-1 caused, when the Java program is running, when reading the byte stream that enters the external, if the default read method is used, it is also used to decode the character set of the ISO8859-1, in this process, Chinese characters are no longer the original Chinese characters. That is to say, at this time, we are dealing with Chinese characters, but a pair of garbled characters, although garbled characters, but the information is not lost, after processing, after a ISO8859-1 encoding, and restored to the normal gb18030 encoding output, all no garbled. Our previous solution was to specify the parameter-encoding ISO8859-1 when compiling the original file, let the compiler use the character set of the ISO8859-1 to decode the source file compilation, and then run the program, at this time, the internal Chinese strings of the output program are not garbled. It seems that everything has been solved, but it has not fundamentally solved the problem. The class file is much larger than normal. The more Chinese characters are used in the program, the faster the class file grows. In addition, the Chinese information becomes more appealing.
 
Another problem is that if we compile a program normally and set it to a Chinese environment on the AIX system, and then run the Java program, this will neither make the program larger nor make the Chinese language taste better, however, after a while, I found the problem again. If a remote Chinese text is still garbled during the processing, the reason is that the Chinese Character Set of Aix is IBM-euccn, I think it may be that this character set lacks a remote Chinese character and cannot be interpreted, so remote characters become garbled.
 
The final solution is to compile the original program normally in Windows Chinese environment, compile and generate the class file by default, or specify the parameter-encoding gb18030 during compilation, in this way, Chinese characters can be interpreted and converted to UTF-8 stored in the class file, at runtime, we need to develop parameters, Java-dfile. encoding = gb18030 ....., the system environment uses the default English language, so that the JVM will not set the default character set according to the system environment, but all input and output will use the character set we specified, this not only solves the problem of Chinese output in the English environment, but also solves the problem of remote word display.
 
The transcoding process of Chinese characters is attached:
1. gb18030 of 'Chinese' is encoded as D6 D0 ce C4 and corresponds to the Java source code file.
After ISO8859-1 decoding, The UTF-8 is 81 30 89 30 81 30 88 34 81 30 88 32 81 30 87 32.
The process of outputting Chinese characters by Java program corresponding to d6 D0 ce C4 encoded by ISO8859-1
D6 D0 ce C4 is interpreted by gb18030 as a process in which the Chinese characters are displayed in the system.
In this process, although garbled characters appear in the middle, but the information is not lost, it can still be restored to Chinese, which is a rather poor process.
 
2. The UTF-8 code of the 'Chinese' is E4 B8 ad E6 96 87, which corresponds to the normally compiled class file storage content.
After the ISO8859-1 code 3f 3f, has error, loss of information, corresponding to the Java program Chinese character output process
3f 3f is interpreted as Chinese characters ??, Garbled and cannot be restored. The system displays Chinese characters.
In this process, information loss is a completely incorrect process.
 
3. gb18030 encoded as D6 D0 ce C4 corresponds to the Java source code file.
After gb18030 decoding, The UTF-8 is E4 B8 ad E6 96 87.
After gb18030 encoding to d6 D0 ce C4, the Java program outputs too long Chinese characters.
D6 D0 ce C4 is interpreted by gb18030 as a process in which the Chinese characters are displayed in the system.
This process is the most ideal process, with no information loss or poor information.

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.