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这个属性,后续的读写操作没有特殊编码需要的划,都可以继承过来使用.

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值