java -Dfile.encoding设置解决程序运行乱码问题

-Dfile.encoding解释:
在命令行中输入java,在给出的提示中会出现-D的说明:
-D<name>=<value>
               set a system property
-D后面需要跟一个键值对,作用是通过命令行向java虚拟机传递一项系统属性
对-Dfile.encoding=UTF-8来说就是设置系统属性file.encoding为UTF-8
那么file.encoding什么意思?字面意思为文件编码。
搜索java源码,只能找到4个文件中包含file.encoding的文件,也就是说只有四个文件调用了file.encoding这个属性。
在java.nio.charset包中的Charset.java中。这段话的意思说的很明确了,简单说就是默认字符集是在java虚拟机启动时决定的,依赖于java虚拟机所在的操作系统的区域以及字符集。
代码中可以看到,默认字符集就是从file.encoding这个属性中获取的。

 

=====

Java's file.encoding property on Windows platfor 
This property is used for the default encoding in Java, all readers and writers would default to using this property. file.encoding is set to the default locale of Windows operationg system since Java 1.4.2. System.getProperty("file.encoding") can be used to access this property. Code such as System.setProperty("file.encoding", "UTF-8") can be used to change this property. However, the default encoding can be not changed dynamically even this property can be changed. So the conclusion is that the default encoding can't change after JVM starts. java -dfile.encoding=UTF-8 can be used to set the default encoding when starting a JVM. I have searched for this option Java official documentation. But I can't find it.

posted @ 2012-04-11 16:29  vigarbuaa  阅读(8094)  评论(0编辑  收藏  举报