加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Properties文件的XML格式

发布时间:2020-12-16 00:40:04 所属栏目:百科 来源:网络整理
导读:想必大家都用过*.properties文件,作为配置文件。但是,如果该文件写入了中文,待编译后内容就会成为乱码,使用native命令也好、使用ant执行编码转换也好,多少有点麻烦,与其如此,我们不如直接使用properties的xml格式。 Xml代码 ? xml version = "1.0" en
想必大家都用过*.properties文件,作为配置文件。但是,如果该文件写入了中文,待编译后内容就会成为乱码,使用native命令也好、使用ant执行编码转换也好,多少有点麻烦,与其如此,我们不如直接使用properties的xml格式。
Xml代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <!DOCTYPEpropertiesSYSTEM"http://java.sun.com/dtd/properties.dtd">
  3. <propertiescomment>系统配置</entry
  4. key="logo.location"><![CDATA[/image/logo/]]>entry key="mail.host"<![CDATA[webmaster@zlex.org]]> key="site.name"<![CDATA[zlex中文网站]]> key="welcome"<![CDATA[欢迎您,{0}!]]>>

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

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


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


因为使用XML格式,不受系统编译影响,不存在中文问题!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读