这个话题来自: Nutz的​​issue 361​

在考虑这个issue时, 我一直倾向于使用系统变量file.encoding来改变JVM的默认编码.

今天,我想到, 这个系统变量,对JVM的影响到底有多大呢? 我使用最简单的方法看看这个变量的影响--在JDK 1.6.0_20的src.zip文件中,查找包含file.encoding字眼的文件. 共找到4个, 分别是: 先上重头戏 java.nio.Charset类:

publicstaticCharset(){if(defaultCharset ==null){synchronized(Charset.class){.security.PrivilegedAction=newGetPropertyAction("file.encoding");String=(String)AccessController.doPrivileged(pa);Charset=(csn);if(cs !=null)=;else=("UTF-8");}}return;}

java.net.URLEncoder的静态构造方法,影响到的方法 java.net.URLEncoder.encode(String)

static{=newBitSet(256);int;for(i ='a';<='z';++){.set(i);}for(i ='A';<='Z';++){.set(i);}for(i ='0';<='9';++){.set(i);}.set(' ');/* encoding a space to a + is done
* in the encode() method */.set('-');.set('_');.set('.');.set('*');=(String)AccessController.doPrivileged (newGetPropertyAction("file.encoding"));}

com.sun.org.apache.xml.internal.serializer.Encoding的getMimeEncoding方法(209行起)

staticString(String){if(null==){try{// Get the default system character encoding.  This may be// incorrect if they passed in a writer, but right now there// seems to be no way to get the encoding from a writer.=System.getProperty("file.encoding","UTF8");if(null!=){/*
* See if the mime type is equal to UTF8. If you don't
* do that, then convertJava2MimeEncoding will convert
* 8859_1 to "ISO-8859-1", which is not what we want,
* I think, and I don't think I want to alter the tables
* to convert everything to UTF-8.
*/String=(encoding.equalsIgnoreCase("Cp1252")||.equalsIgnoreCase("ISO8859_1")||.equalsIgnoreCase("8859_1")||.equalsIgnoreCase("UTF8"))?:(encoding);=(null!=)?:;}else{=;}}catch(SecurityException){=;}}

最后一个javax.print.DocFlavor类的静态构造方法:

static{=(String)java.security.AccessController.doPrivileged(new.security.action.GetPropertyAction("file.encoding"));}

可以看到,系统变量file.encoding影响到
1. Charset.defaultCharset() Java环境中最关键的编码设置
2. URLEncoder.encode(String) Web环境中最常遇到的编码使用
3. com.sun.org.apache.xml.internal.serializer.Encoding 影响对无编码设置的xml文件的读取
4. javax.print.DocFlavor 影响打印的编码

故,影响还是很大的哦, 可以说是Java中编码的一个关键钥匙!​