您的位置:首页 > 编程语言 > Java开发

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

2013-07-31 10:49 579 查看
-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 platform

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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐