Java Chinese programming and configuration experience

Source: Internet
Author: User

Java Chinese programming and configuration experience

Java has a long history in Chinese, and it has never been completely solved yet. However, there are policies and countermeasures that we can always solve. There are two main types of Chinese problems related to Java: programming issues, I/O and internal code conversion. The second type is the configuration of the Java Runtime Environment, involving the font and attribute configuration. I spent just one day solving these problems and felt it was necessary to write a memorandum for myself.
I think we should start with the problem. This will not let everyone sleep. I want to write a program. The basic function of this program is to display the file content. I use JTextArea to do the display. The program is simple, but Chinese characters are garbled. My configuration is JBuilder7 and JBuilder comes with JDK1.3.3 _ b24. My own JDK is JDK1.4.0 _ 02_b02, which is the mainstream JDK. The operating system is an English Windows plus Chinese support package.
I try to change JDK, 1.3.3 and 1.4.0 are not good, down a latest j2sdk-1_4_1-rc is not good, it seems not JDK problem, so I focus on the I/O encoding conversion, I read a number of articles on JAVA Chinese on the internet, and I made it clearer about encoding conversion. But how can I try it? I can't do anything about encoding, but it shows worse, at the beginning, I still had some messy rules, as shown in the English-only system. I still know that it is a Chinese character, but it cannot be displayed. I will wait for the encoding, it becomes a question mark. Alas, depressed!


Coding conversion experience:
JAVA uses UNICODE encoding internally. If Reader/Writer is used in I/O, encoding conversion is required. The system attribute file. encoding is used as the encoding method. If Stream is used, there will be no conversion. It is Binary data.
The following are useful methods: 1. Add the encoding option to Reader/Writer. Pay attention to the encoding direction. encoding in Reader indicates converting data from encoding to Unicode. writer converts Unicode characters to encoding format. 2. Use String. getByte () to convert the String to the specified encoding.
Common encoding format: ISO8859_1, which is the default 8-bit encoding in the English system. Because it is 8-bit, it will not delete the high positions of Chinese characters, therefore, it can also be used to process Chinese characters. (I think it is inappropriate, but I don't know where it is. I still hope you can point it out ). GB2312, GBK, and Chinese character encoding are recommended. It is compatible with GB2312 and supports more Chinese characters. UNICODE, a large character set. I wonder if it is supported by international standards. It uses 16-bit encoding for each character. Although Chinese characters are suitable, English is compromised, it takes more than twice the storage space, so many people are reluctant to write programs that do not support UNICODE.
There are two solutions to the Chinese issue of JSP/Servlet: 1. If you do not perform encoding conversion in the program, you can use javac-encoding GBK *. java to compile all the beans and add them to the JSP page.

Or Add the following directly in HTML:

If you want to add one, you will be able to try it and I will not be able to figure it out.
2. Specify the encoding in the program, use javac-encoding ISO8859_1 *. java to compile all beans, and add
Str = new String (str. getBytes ("ISO8859_1 "));
The above two methods cannot be mixed, that is, either GBK or ISO8859_1. It is the same from the inside out to the outside.
The Chinese language of the database JDBC is usually converted according to the encoding specified by the database, for example, reading according to ISO8859_1 and writing according to ISO8859_1. Generally, there is no problem.

Although I have some experience in coding, it cannot solve my problem. It seems that I use ISO8859_1 for program input and output, and my problem has nothing to do with coding. Is it about font? Among the Swing components, there are always a few fonts, which are basically fixed and cannot be selected. But I suddenly found that the font can be changed. properties file. Generally, JDK contains a Chinese font configuration file, which may be font. properties. zh and so on. JDK names of different versions are different. What you need to do is to overwrite the font with Chinese configurations. properties file. I thought I was successful with joy, but failure hit me again relentlessly. This method is not correct, but in Windows, java can automatically check your system encoding and use the most appropriate font configuration file, which generally does not need to be changed, this was the case before JDK. No wonder that article was the document of JDK.
Even font. properties does not work. Alas, is there a problem with JBuilder? At this point, I had to doubt it. Although it never disappointed me, I turned off JBuilder and picked up the JDK command line. Oh! Cute Chinese! It's just... it's confusing, because JBuilder also uses this JDK? No reason. I open JBuilder again and run it. It's garbled! Strange things ....... let me see ..... I copied the execution COMMANDS IN JBuilder to the DOS window and manually executed them .... no Chinese ..... strange and strange: I carefully compare the commands I typed with the commands copied by JBuilder. Except for java, java is used, and almost everything else is the same, is it ....... is java different from java W?
Alas ....... what should I say? The answer is this. javaw will use different Local configurations from java, so that I can never see Chinese, and java is good. JBuilder is also good, no problem. It is not its fault to call javaw. Do you want to pop up a DOS black box every time you run or debug it? Haha. This is a JDK Bug. I found this bug on the java website and described it in the same way as mine. Then Sun said the Bug has been fixed. Hey, it's just a nonsense! Fix eggplant! Bug no.: 4629351. If you don't believe it, you can check it out.
But can't I see my cute Chinese in JBuilder? It seems that JBuilder5 can still choose whether to use java or javaw. Now it seems that I can't choose it. How can we make javaw support Chinese? After another hard search, there was no result, so I was depressed! Just as it was getting bored, I suddenly remembered that TOMCAT was originally configured under UNIX. It seemed that I had added some startup parameters to make tomcat support Chinese characters. I quickly pulled out the previous documents, fortunately, I first wrote my work experience, or else I caught myself blind again. In fact, it is very simple to add some attributes to the Java Virtual Machine:
-Dfile. encoding = GBK-Ddefault. client. encoding = GBK-Duser. language = zh-Duser. region = CN
Keep it following the javaw command line as a parameter. Haha, that's done!
In JBuilder, select Project-> Project Properties-> run-> edit-> VM Parameters and fill in the Parameters!

Configuration tips:
In lower JDK versions and UNIX versions, you need to modify font. properties so that the java vm can find appropriate fonts to display Chinese characters.
So far, Java W has a Bug. Only Local English is used. You need to add attributes to the VM to display Chinese characters normally.

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.