zoukankan      html  css  js  c++  java
  • Properties文件的XML格式(转)

    想必大家都用过*.properties文件,作为配置文件。但是,如果该文件写入了中文,待编译后内容就会成为乱码,使用native命令也好、使用ant执行编码转换也好,多少有点麻烦,与其如此,我们不如直接使用properties的xml格式。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
    <properties>
        <comment>系统配置</comment>
        <entry
            key="logo.location"><![CDATA[/image/logo/]]></entry>
        <entry
            key="mail.host"><![CDATA[webmaster@zlex.org]]></entry>
        <entry
            key="site.name"><![CDATA[zlex中文网站]]></entry>
        <entry
            key="welcome"><![CDATA[欢迎您,{0}!]]></entry>
    </properties>

    对应原有的properties文件

    1
    2
    3
    4
    5
    #系统配置
    logo.location=/image/logo/
    mail.host=webmaster@zlex.org
    site.name=zlex中文网站
    welcome=欢迎您,{0}!

    这里需要替换{0},可以使用MessageFormat,参考如下代码:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    private FileInputStream fis;
     
        @Before
        public void init() {
            try {
                fis = new FileInputStream(new File("config.xml"));
            } catch (Exception e) {
                e.printStackTrace();
                fail(e.getMessage());
            }
        }
     
        @Test
        public void t() {
            Properties properties = new Properties();
            try {
                properties.loadFromXML(fis);
                System.err.println(MessageFormat.format(
                        (String) properties.get("welcome"), "snowolf"));
            } catch (Exception e) {
                e.printStackTrace();
                fail(e.getMessage());
            }
        }

    得到控制台输出:

    1
    欢迎您,snowolf!
  • 相关阅读:
    css和js实现硬件加速渲染自定义滚动条
    入驻博客园
    原生JS实现动态折线图
    纯JS自定义网页滚动条
    Unity实现代码控制音频播放
    MVC实现修改数据
    JS实现将二维数组生成到页面上
    JS实现页面计时
    多中方式,实现斐波那契数列
    算法题
  • 原文地址:https://www.cnblogs.com/sparkbj/p/5808071.html
Copyright © 2011-2022 走看看