使用配置文件方式解决工程中文乱码问题

建立文件build.cmd,内容为:native2ascii -encoding GBK ApplicationResources_source.properties ApplicationResources.properties

将ApplicationResources_source.properties文件转化为asc编码的ationResources.properties

============

出自:

java程序读取properties配置文件出现中文乱码

2010-05-19 18:22 提问者: javanewlearner | 浏览次数:9047次
我编写了一个很简单的程序,读取配置文件(.properties)里的数据。主要代码部分如下:InputStream in = new FileInputStream("E:\\张兆祥_软件开发工具\\开发工具安装后的\\eclipse\\事例程序\\dbexamples\\bin\\chapter\\user.properties");
   Properties property=new Properties();
   property.load(in);
   String username=property("username");
   String password=property.getProperty("password");
   pstmt.setString(1,username);
   pstmt.setString(2,password);
   pstmt.executeUpdate();
其中的pstmt.set...为插入数据库时的语句,但是当我改成向控制台输出的时候依然是乱码password是英文,所以没问题,就是那个username,因为是中文,输出为:???×?é不知道该怎么解决,求教哪个高手赐教!!
我来帮他解答
满意回答
2010-05-19 21:59
这个问题有两种办法:
第一种办法:如楼上所说的那样也可以,就是native2ascii -reverse -encoding gb2312 user.properties ActionName_zh_CN.properties这样以后,你打开ActionName_zh_CN.properties的内容,再将ActionName_zh_CN.properties文件这样置:native2ascii ActionName_zh_CN.properties userChange.properties 然后你的程序读userChange.properties的内容就可以。
第二种办法是:
如你代码里写的,你可以在String username=property.getProperty("username");之后,添加JAVA代码页可以将乱码转为中文的。用如下语句就可以了,resultName=new String(username.getBytes("ISO-8859-1"),"gbk"); 然后再用resultName就可以了,不过这样的话你下面的String password=property.getProperty("password");
都慢慢的通过上面的java代码去转。 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值