Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Chinese programming and configuration of Java

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you the "Java Chinese programming and configuration experience example analysis", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "Java Chinese programming and configuration experience sample analysis" this article.

Experience of Chinese programming and configuration of Java

The Chinese problem of Java has a long history and has not been completely solved so far, but there are policies and countermeasures, and we always have ways to solve it. There are two main types of Chinese problems related to Java, one is programming problems, which involve Imax O, internal code conversion and so on. The second category is the configuration of the Java runtime environment, involving fonts, attribute configuration and so on. I just spent a day solving these problems and felt it necessary to write a memo to myself or something.

I think we'd better start with the problem, so as not to make everyone 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 thing, the program is simple to home, but the Chinese is garbled. My configuration is jdk1.3.3_b24 that comes with JBuilder7,JBuilder, and my own JDK is JDK1.4.0_02_b02, which is the mainstream JDK. The operating system is English windows2000 plus Chinese support package.

I tried to change JDK,1.3.3 and 1.4.0 is not good, down is not the latest j2sdk-1_4_1-rc, it does not seem to be the problem of JDK, so I focused on the transcoding of JAVA O, I checked a number of online articles on Chinese issues of JAVA, and made the transcoding very clear, but how to try, what kind of coding can not be changed, but the display is even worse. At the beginning, there were still some rules, as shown on the pure English system, at least I knew it was a Chinese character, but it couldn't be displayed, and when I tweaked the code, it became a question mark. Alas, depressed!

Experience of transcoding:

The inside of JAVA is UNICODE coding. If Reader/Writer is used in Imax O, the transcoding will occur. Use the system attribute file.encoding as the encoding method. If you use Stream, there is no conversion, that is the data of Binary.

Useful methods are: 1. Add the option of encoding to Reader/Writer and pay attention to the direction of encoding. Encoding in Reader means that converting data from encoding to Unicode,writer is to convert Unicode characters into encoding format. two. Use String.getByte () to convert the string to the specified encoding.

Commonly used coding format: ISO8859_1, this is the default 8bit coding in the English system, because it is 8bit, so the high order of Chinese characters will not be deleted, so it can also be used to deal with Chinese characters (I always feel something wrong with my own understanding, but I don't know where it is, and I still hope that it will be pointed out). GB2312 and GBK, Chinese character coding, GBK is recommended, it is compatible with GB2312 and supports more Chinese characters. UNICODE, a large character set, I do not know whether it is an international standard that we all support anyway, using 16-bit encoding for each character, although Chinese characters are just appropriate, but English has suffered, and we have to use twice as much space to store it, so many people are still unwilling to write programs that do not support UNICODE.

There are two solutions to the Chinese problem of JSP/servlet: 1. Instead of transcoding in the program, leave the work to the browser by compiling all the bean with javac-encoding GBK * .java, and then adding it to the JSP page

Or add: directly to HTML:

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

two. Specify the code in the program, compile all bean with javac-encoding ISO8859_1 * .java, and add to the programs that involve Chinese display

Str=new String (str.getBytes ("ISO8859_1"))

The above two methods cannot be mixed, which means that it is either GBK or ISO8859_1, which is the same from the inside out.

Database JdbC Chinese problems, generally as long as the database specified by the code conversion, such as according to ISO8859_1 read, ISO8859_1 write, generally there is no problem.

Although there are these coding experience, but it can not solve my problem. It seems that both the input and output of my program use ISO8859_1, and my problem has nothing to do with coding. Is it the font problem? In the components of swing, there are always a few fonts, which are basically fixed, and you can't even choose that one. But I suddenly found that you can change the configuration of these fonts, that is, font.properties this file, generally JDK has a Chinese font configuration file, may be font.properties.zh and so on, different versions of JDK names are different, what you need to do is to overwrite the font.properties file with Chinese configuration. I thought with joy that I had succeeded, but the failure mercilessly hit me again. This method is not wrong, but in the Windows system, java can automatically check your system coding, use the most appropriate font configuration file, generally do not need you to change, before the JDK1.2 is really to change, no wonder that article is a JDK1.1 document.

Even font.properties doesn't work. Alas, is there something wrong with JBuilder? When I got here, I had to doubt it, although it never let me down. I turned off JBuilder and picked up the JDK command line. Oops! Lovely Chinese has come out! It is simply. Inexplicable, because JBuilder also uses this JDK? There is no reason, I open JBuilder again, run, garbled! Strange things. Let me have a look. I copy the execution commands in JBuilder to the DOS window and execute them manually. There is no Chinese. Strange, I carefully compare the commands tapped by my hands with those copied by JBuilder, except that I use java which uses javaw, everything else is almost the same. How can this java and javaw be different?

Alas. What do you want me to say, the answer is this. Javaw will work with a different Local configuration from java, so I can't see Chinese all the time, and java is fine. JBuilder is fine, no problem, and it's not its fault to call javaw. Do you want to pop up a DOS black box every time you run or debug? He he. This is a bug of JDK. I looked up this bug on java's website and described the situation as mine, and then Sun said that the Bug had been repaired. Hey, this is bullshit! Fix an eggplant! The Bug number is 4629351. If you don't believe me, you can check it out.

But can't I read my lovely Chinese in JBuilder? It seems that JBuilder5 can still choose whether to use java or javaw, but now there seems to be no way to choose. How can we get javaw to support Chinese? After another hard search, there was no result, depressed! When I was depressed, I suddenly remembered that I had configured tomcat under unix. It seemed that I had added some startup parameters to make tomcat support Chinese. I quickly turned out the previous documents. Ah, fortunately, I wrote my work experience, otherwise I would have gone blind again. In fact, the reason is very simple, is to add some properties to the JAVA virtual machine:

-Dfile.encoding=GBK-Ddefault.client.encoding=GBK-Duser.language=zh-Duser.region=CN

Follow it to the command line of javaw as an argument, , done!

Select Project- > Project Properties- > run- > edit- > VM Parameters in JBuilder and fill it in!

Configuration experience:

In the lower version of JDK and UNIX, the font.properties needs to be changed so that JAVA VM can find the right font to display Chinese characters.

So far, javaw has Bug, only English Local is used, and you need to add attributes to VM to display Chinese normally.

The above is all the contents of the article "sample Analysis of Java programming and configuration in Chinese". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 248

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report