Let you not worry: Java Chinese programming configuration experience

Source: Internet
Author: User
Tags character set command line java web tomcat
Programming | experience | Chinese

  Experience of Java programming and configuration in Chinese

Java has a long history of Chinese problems, continuous, has not been completely resolved, but there are policies under the policy, we always have the means to deal with it. Java-related Chinese problems are mainly in two categories, one is programming problems, involving I/O, internal code conversion. The second category is the configuration of the Java Runtime environment, involving fonts, property configuration, and so on. I have just spent a day solving these problems and feel the need to write myself a memo or something.

I think we should start with the problem so that we don't get everyone to doze off. I want to write a program, this program has a basic function is to display the contents of the file, I use JTextArea to do the display of things, the program simple home, but is the Chinese are garbled. My configuration is Jbuilder7,jbuilder's own jdk1.3.3_b24, I installed the JDK is JDK1.4.0_02_B02, are the mainstream JDK. The operating system is an English Windows2000 plus Chinese support package.

I try to change jdk,1.3.3 and 1.4.0 neither, down a new J2SDK-1_4_1-RC is not a problem, as if not JDK, so I focused on the I/O code conversion, I looked at some of the web on the Java Chinese problem articles, To make the code conversion more clear, but how to try, change what code toss not, but show worse, the original or disorderly some rules, is like in the pure English system on display, at least I also know that it is Chinese characters, just do not show, and so I toss code, it becomes a question mark. Alas, depressed!

  Code Conversion Experience:

Java internal is Unicode encoding, when I/O, if the use of Reader/writer will occur encoding conversion, using System Properties file.encoding as encoding. If you use the stream there is no conversion, that is the binary data.

Useful methods are: 1. The encoding option on the Reader/writer, note the direction of the encoding, and the encoding in reader indicates that the data is converted from encoding to Unicode. Writer is the conversion of Unicode characters to encoding format. 2. Converts a string to the specified encoding using String.getbyte ().

Commonly used encoding formats: Iso8859_1, this is the English system default 8bit code, because it is 8bit, so will not delete the high level of Chinese characters, so it is also possible to deal with Chinese characters (I understand it, always feel something wrong, but do not know where the wrong, but also hope that the expert pointed out). GB2312 and GBK, encoding, recommend the use of GBK, which is compatible with GB2312 and supports more Chinese characters. UNICODE, a large character set, I do not know is not international standards anyway, we all support the use of 16-bit for each character encoding, although the Chinese character is appropriate, but the English is a disadvantage, to use more than one time to store, so many people or the boss is not willing to write programs that do not support Unicode

There are two solutions to Jsp/servlet's Chinese problems: 1. Do not encode the conversion in the program, the work to the browser, the method is to use javac–encoding GBK *.java to compile all the beans, and then on the JSP page add

<%@ page contenttype= "text/html;charset=gb2312"%>

<%@ page contenttype= "text/html;charset=gb2312"%>

  or add it directly in HTML:

In the end add that, try to know, I also do not understand.

2. Specify the encoding in the program, compile all the beans with javac–encoding iso8859_1 *.java, and add

Str=new String (str.getbytes ("iso8859_1"));

The above two methods can not be mixed, meaning is either GBK, or iso8859_1, from the inside to the same as good.

Database jdbc Chinese problem, generally as long as the database specified by the code to convert, such as according to Iso8859_1 Read, iso8859_1 write, generally there is no problem.

Although I have these coding experience, it doesn't solve my problem. It seems that my program input and output are all iso8859_1, and my problem has nothing to do with coding. Is it a font problem? In swing components, the font is always a few, basically is dead, choose that is not. But I suddenly found that you can change the configuration of these fonts, is font.properties this file, the general JDK with the Chinese font profile, It may be font.properties.zh or something, different versions of the JDK name is a little different, all you have to do is overwrite the font.properties file with the Chinese configuration. I was full of joy to think of success, but the failure of the ruthless once again hit me. Not this method is wrong, but in Windows system, Java can be more automatic check your system code, using the most appropriate font profile, generally do not need you to change, in the JDK1.2 is really to be so changed, no wonder that the article is JDK1.1 document.

Even font.properties also not spirit, alas, is jbuilder have a problem? Here, I have to doubt it, although it has never let me down, I turned off the JBuilder, picked up the JDK command line, Ah! The lovely Chinese has come out! Inexplicably, because JBuilder is also used this JDK? No reason, I open jbuilder again, run, garbled! Strange things ... Let me see ... I copy the execution command inside the JBuilder to the DOS window to manually execute .... No Chinese ... Strange, I carefully control my hand on the command and JBuilder of the command, except that I use Java it uses is JAVAW, the other almost all the same, don't .... Is this Java and JAVAW going to be different?

Alas....... What I'm going to say, the answer is this, JAVAW will work with a different local configuration than Java, causing me to never see Chinese, and Java is fine. JBuilder is also good, no problem, call JAVAW is not its fault, do you want to run or debug every time you jump out of a DOS black box? hehe. This is a JDK bug, I found this bug on the Java Web site, described the same as mine, and then Sun said the bug has been fixed, hey, it is nonsense! Fix an Eggplant! The bug number is: 4629351. You can go and see if you don't believe me.

But can't I see my lovely Chinese in jbuilder? It seems as if JBUILDER5 can choose whether to use Java or JAVAW, now it seems impossible to choose. How can I get JAVAW to support Chinese? After another hard to find, unexpectedly no results, depressed! In a time of anguish, it suddenly occurred to me that the Tomcat, which had been configured under UNIX, seemed to have added some startup parameters to enable Tomcat to support Chinese, and quickly turned out the previous document, Ah, Luckily I had written the work experience, otherwise flying blind again. In fact, the simple reason is to add some attributes to the Java Virtual machine:

-dfile.encoding=gbk-ddefault.client.encoding=gbk-duser.language=zh-duser.region=cn

Put it in the back of the JAVAW command line when the parameters, haha, fix!

In JBuilder choose Project->project properties->run->edit->vm Parameters, fill on the line!

  Configuration experience:

In the JDK version and UNIX, you need to change the font.properties so that the Java VM can find the right font to display Chinese characters.

So far, JAVAW has bugs, only the English local, the VM needs to add attributes to the normal display of Chinese.



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.