Properties文件的XML格式

想必大家都用过*.properties文件,作为配置文件。但是,如果该文件写入了中文,待编译后内容就会成为乱码,使用native命令也好、使用ant执行编码转换也好,多少有点麻烦,与其如此,我们不如直接使用properties的xml格式。
Xml代码 收藏代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <!DOCTYPEpropertiesSYSTEM"http://java.sun.com/dtd/properties.dtd">
  3. <properties>
  4. <comment>系统配置</comment>
  5. <entry
  6. key="logo.location"><![CDATA[/image/logo/]]></entry>
  7. <entry
  8. key="mail.host"><![CDATA[webmaster@zlex.org]]></entry>
  9. <entry
  10. key="site.name"><![CDATA[zlex中文网站]]></entry>
  11. <entry
  12. key="welcome"><![CDATA[欢迎您,{0}!]]></entry>
  13. </properties>

对应原有的properties文件
Properties代码 收藏代码
  1. #系统配置
  2. logo.location=/image/logo/
  3. mail.host=webmaster@zlex.org
  4. site.name=zlex中文网站
  5. welcome=欢迎您,{0}!

这里需要替换 {0} ,可以使用 MessageFormat ,参考如下代码:
Java代码 收藏代码
  1. privateFileInputStreamfis;
  2. @Before
  3. publicvoidinit(){
  4. try{
  5. fis=newFileInputStream(newFile("config.xml"));
  6. }catch(Exceptione){
  7. e.printStackTrace();
  8. fail(e.getMessage());
  9. }
  10. }
  11. @Test
  12. publicvoidt(){
  13. Propertiesproperties=newProperties();
  14. try{
  15. properties.loadFromXML(fis);
  16. System.err.println(MessageFormat.format(
  17. (String)properties.get("welcome"),"snowolf"));
  18. }catch(Exceptione){
  19. e.printStackTrace();
  20. fail(e.getMessage());
  21. }
  22. }


得到控制台输出:
引用
欢迎您,snowolf!



因为使用XML格式,不受系统编译影响,不存在中文问题!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值