版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本声明
http://www.chedong.com/tech/hello_unicode.html
关键词:linux java mutlibyte encoding locale i18n i10n chinese ISO-8859-1 GB2312 BIG5 GBK UNICODE
内容摘要:
不知道你有没有这样的感受:为什么PHP很少有乱码问题而用Java做WEB应用却这么麻烦呢?为什么在Google上能用简体中文查到繁体中文,甚至日文的结果?而且用Google的时候发现它居然能自动根据我使用浏览器的语言选择自动调出中文界面?
很多国际化应用的让我理解了这么一个道理:Unicode是为更方便的做国际化应用设计的,而Java核心的字符是基于UNICODE的,这一机制为应用提供了对中文“字”的控制(而不是字节)。但如果不仔细理解其中的规范,这种自由反而会成为累赘,从而导致更多的乱码问题:
试验1:操作系统语言环境设置对Java应用缺省编码方式的影响
为了了解Java应用的编码处理的机制,首先要了解操作系统对JVM缺省编码方式的影响,因此我做了一个Env.java,用于打印显示不同系统下JVM的属性和系统支持的LOCALE。程序很简单:
/*
* Copyright (c) 2002 Email: chedongATbigfoot.com/chedongATchedong.com
* $Id: hello_unicode.html,v 1.6 2003/11/09 07:57:11 chedong Exp $
*/
import java.util.*;
import java.text.*;
/**
* 目的:
* 显示环境变量和JVM的缺省属性
* 输入:无
* 输出:
* 1 支持的LOCALE
* 2 JVM的缺省属性
*/
public class Env {
/**
* main entrance
*/
public static void main(String[] args) {
System.out.println("Hello, it's: " + new Date());
//print available locales
Locale list[] = DateFormat.getAvailableLocales();
System.out.println("======System available locales:======== ");
for (int i = 0; i < list.length; i++) {
System.out.println(list[i].toString() + "\t" + list[i].getDisplayName());
}
//print JVM default properties
System.out.println("======System property======== ");
System.getProperties().list(System.out);
}
}
最需要注意的是JVM的file.encoding属性,这个属性确定了JVM的缺省的编码/解码方式:从而影响应用中所有字节流==>字符流的解码方式 ,字符流==>字节流的编码方式。
LINUX下的LOCALE可以通过 LANG=zh_CN; LC_ALL=zh_CN.GBK; export LANG LC_ALL 设置。locale 命令可以显示系统当前的环境设置
Windows的LOCALE可以通过 控制面板==>区域设置 设置实现
LANG=en_US LC_ALL=en_USGNU/Linux 2.4.x (J2SE1.3.1)
LANG=zh_CN LC_ALL=zh_CN.GBKWindows 2000(J2SE1.3.0)
区域设置:中国 中文Windows 2000(J2SE1.3.0)
区域设置:英国 英文
Hello, it's: Tue Jul 30 11:05:44 CST 2002
======System available locales:========
en English
en_US English (United States)
ar Arabic
ar_AE Arabic (United Arab Emirates)
ar_BH Arabic (Bahrain)
ar_DZ Arabic (Algeria)
ar_EG Arabic (Egypt)
ar_IQ Arabic (Iraq)
ar_JO Arabic (Jordan)
ar_KW Arabic (Kuwait)
ar_LB Arabic (Lebanon)
ar_LY Arabic (Libya)
ar_MA Arabic (Morocco)
ar_OM Arabic (Oman)
ar_QA Arabic (Qatar)
ar_SA Arabic (Saudi Arabia)
ar_SD Arabic (Sudan)
ar_SY Arabic (Syria)
ar_TN Arabic (Tunisia)
ar_YE Arabic (Yemen)
be Byelorussian
be_BY Byelorussian (Belarus)
bg Bulgarian
bg_BG Bulgarian (Bulgaria)
ca Catalan
ca_ES Catalan (Spain)
ca_ES_EURO Catalan (Spain,Euro)
cs Czech
cs_CZ Czech (Czech Republic)
da Danish
da_DK Danish (Denmark)
de German
de_AT German (Austria)
de_AT_EURO German (Austria,Euro)
de_CH German (Switzerland)
de_DE German (Germany)
de_DE_EURO German (Germany,Euro)
de_LU German (Luxembourg)
de_LU_EURO German (Luxembourg,Euro)
el Greek
el_GR Greek (Greece)
en_AU English (Australia)
en_CA English (Canada)
en_GB English (United Kingdom)
en_IE English (Ireland)
en_IE_EURO English (Ireland,Euro)
en_NZ English (New Zealand)
en_ZA English (South Africa)
es Spanish
es_BO Spanish (Bolivia)
es_AR Spanish (Argentina)
es_CL Spanish (Chile)
es_CO Spanish (Colombia)
es_CR Spanish (Costa Rica)
es_DO Spanish (Dominican Republic)
es_EC Spanish (Ecuador)
es_ES Spanish (Spain)
es_ES_EURO Spanish (Spain,Euro)
es_GT Spanish (Guatemala)
es_HN Spanish (Honduras)
es_MX Spanish (Mexico)
es_NI Spanish (Nicaragua)
et Estonian
es_PA Spanish (Panama)
es_PE Spanish (Peru)
es_PR Spanish (Puerto Rico)
es_PY Spanish (Paraguay)
es_SV Spanish (El Salvador)
es_UY Spanish (Uruguay)
es_VE Spanish (Venezuela)
et_EE Estonian (Estonia)
fi Finnish
fi_FI Finnish (Finland)
fi_FI_EURO Finnish (Finland,Euro)
fr French
fr_BE French (Belgium)
fr_BE_EURO French (Belgium,Euro)
fr_CA French (Canada)
fr_CH French (Switzerland)
fr_FR French (France)
fr_FR_EURO French (France,Euro)
fr_LU French (Luxembourg)
fr_LU_EURO French (Luxembourg,Euro)
hr Croatian
hr_HR Croatian (Croatia)
hu Hungarian
hu_HU Hungarian (Hungary)
is Icelandic
is_IS Icelandic (Iceland)
it Italian
it_CH Italian (Switzerland)
it_IT Italian (Italy)
it_IT_EURO Italian (Italy,Euro)
iw Hebrew
iw_IL Hebrew (Israel)
ja Japanese
ja_JP Japanese (Japan)
ko Korean
ko_KR Korean (South Korea)
lt Lithuanian
lt_LT Lithuanian (Lithuania)
lv Latvian (Lettish)
lv_LV Latvian (Lettish) (Latvia)
mk Macedonian
mk_MK Macedonian (Macedonia)
nl Dutch
nl_BE Dutch (Belgium)
nl_BE_EURO Dutch (Belgium,Euro)
nl_NL Dutch (Netherlands)
nl_NL_EURO Dutch (Netherlands,Euro)
no Norwegian
no_NO Norwegian (Norway)
no_NO_NY Norwegian (Norway,Nynorsk)
pl Polish
pl_PL Polish (Poland)
pt Portuguese
pt_BR Portuguese (Brazil)
pt_PT Portuguese (Portugal)
pt_PT_EURO Portuguese (Portugal,Euro)
ro Romanian
ro_RO Romanian (Romania)
ru Russian
ru_RU Russian (Russia)
sh Serbo-Croatian
sh_YU Serbo-Croatian (Yugoslavia)
sk Slovak
sk_SK Slovak (Slovakia)
sl Slovenian
sl_SI Slovenian (Slovenia)
sq Albanian
sq_AL Albanian (Albania)
sr Serbian
sr_YU Serbian (Yugoslavia)
sv Swedish
sv_SE Swedish (Sweden)
th Thai
th_TH Thai (Thailand)
tr Turkish
tr_TR Turkish (Turkey)
uk Ukrainian
uk_UA Ukrainian (Ukraine)
zh Chinese
zh_CN Chinese (China)
zh_HK Chinese (Hong Kong)
zh_TW Chinese (Taiwan)
======System property========
-- listing properties --
java.runtime.name=Java(TM) 2 Runtime Environment, Stand...
sun.boot.library.path=/usr/java/jdk1.3.1_04/jre/lib/i386
java.vm.version=1.3.1_04-b02
java.vm.vendor=Sun Microsystems Inc.
java.vendor.url=http://java.sun.com/
path.separator=:
java.vm.name=Java HotSpot(TM) Client VM
file.encoding.pkg=sun.io
java.vm.specification.name=Java Virtual Machine Specification
user.dir=/home/chedong/src/char_test
java.runtime.version=1.3.1_04-b02
java.awt.graphicsenv=sun.awt.X11GraphicsEnvironment
os.arch=i386
java.io.tmpdir=/tmp
line.separato