file.encoding

參考鏈接

 

http://blog.csdn.net/zhuyijian135757/article/details/37706437

 

http://blog.csdn.net/elia1208/article/details/6329428

 

http://blog.csdn.net/huoyunshen88/article/details/25896677

 

http://code-chris.iteye.com/blog/365157

 

 

1.tomcat環境中file.encoding引發的思考

 

編碼問題總結:

 

linux 系統默認編碼utf-8

修改和查看linux編碼問題:

[html] view plaincopy在CODE上查看代碼片派生到我的代碼片

[root@test-1 ~]# vi /etc/sysconfig/i18n  

LANG="en_US.UTF-8"  

SYSFONT="latarcyrheb-sun16"  

linux默認編碼UFT-8,終端輸出默認編碼自然是UTF-8,window中使用secureCRT默認編碼是GBK,

鏈接到linux系統輸出亂碼,因爲Linux默認是utf-8,所以把secureCRT的默認編碼修改爲utf-8就ok。

 

WINDOWS 默認編碼GBK

瞭解了系統平臺的的編碼,才容易解決編碼問題。

WIN7中tomcat啓動的控制檯默認編碼是GBK,一般軟件默認採用系統默認字符集。

tomcat的應用一般常用的國際編碼是utf-8,應用輸出採用系統默認編碼GBK,

所以亂碼,再啓動jvm時設置成-Dfile.encoding="UTF-8",應用亂碼問題解決。

 

 

 

file.encoding默認的字符集跟操作系統有關,中文操作系統下面默認的字符集是GBK,如果流程定義的xml文件中用UTF-8,

 

則不能正確轉換,所以需要修改file.encoding的值爲UTF-8。

 

部署在tomcat中的文件要修改file.encoding的值,可以在tomcat的catalina.bat文件中set JAVA_OPTS=%JAVA_OPTS% %LOGGING_CONFIG%的後面加上 -Dfile.encoding="UTF-8"

 

 

java File_encoding屬性

windows下一般是GBK. 指定編碼方式也很簡單,  java  -Dfile.encoding=utf-8   xxxx (需要執行的class文件)

 

下面來看下 file.encoding 這個屬性的英文解釋.

 

This property is used for the default encoding in Java, all readers and writers would default to use 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 not be changed dynamically even this property can be changed. So the conclusion 

 

is that the default encoding can’t be changed 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.

 

 

大致的意思主要下面幾點:

 

1. java內所有的reader和 writer操作默認都是用 file.encoding這個系統屬性作爲編碼方式的,看代碼:

 

[java] view plaincopy

//way1  

String html1="<html>...</html>";  

FileWriter writer1=new FileWriter(new File("C:\\xxxx.html"));  

writer1.write(html1);  

writer1.close();  

  

//way2  

String html2="<html>...</html>";  

OutputStreamWriter writer2=new OutputStreamWriter(new FileOutputStream  

        (new File("C:\\xxxx.html")),"utf-8");  

writer2.write(html2);  

writer2.close();  

 

 

 

第一種方法默認會用 file.encoding 這個屬性對文件進行編碼,然後輸出.一旦你執行class文件的時候沒有指定該屬性, 默認就會用操作系統本身編碼方式,如gbk等.

第二種方式指定了文件編碼方式,並輸出.

 

偶項目中的遇到異常就是由第一種方法導致的,剛開始我用第二種方式去解決的,但是這隻能解決這一地方,其他沒發現的就不好解決了. 更好的解決,看注意點2.

 

2.JVM啓動之前如果未指定file.encoding這個屬性,這個屬性就會默認爲操作系統編碼方式, JVM啓動如果指定了file.encoding這個屬性,整個項目都會用這個屬性

 

作爲reader和writer操作的默認編碼方式,並且在一個運行的應用程序中

 

file.encoding的值只有一個,並且值爲入口函數的保存編碼的值,不會被後面的覆蓋。

 

so,解決問題最好的方式就是在啓動項目時就知道file.encoding這個屬性,後續的讀寫操作沒有特殊編碼需要的劃,都可以繼承過來使用.

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章