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.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM