1. 适用环境:

当前,在做ssh框架,国际化等各种Java项目的时候,常常需要使用到lo4j.properties,en_us.properties 之类,它们是怎么起作用的呢? 找了点资料,尝试了下,发现它装载配置的确挺简单的

2. 读:

待读取/解析的数据可以放在.properties文件中,也可以放在xml文件中,过程基本上大同小异.一次干掉两个,Java真是不挑.

.properties

数据如下: sample.properties

foo0=data0
foo1=data1

另外,下面这么写也是合法并且和上面等价的:

fo
 o0=data0
foo1=dat
       a1

解析如下:

package com.argcandargv.sample;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class ParseProperties{
    public static void main(String []args) throws IOException{
        //absolute path
        String fileName="/home/yu/KEG/test/java/PropertiesUsage/sample.properties";
        Properties properties = new Properties();
        FileInputStream fileInpustStream = new FileInputStream(fileName);
        properties.load(fileInpustStream);
        fileInpustStream.close();
        properties.list(System.out);
        System.out.println("properties of foo1 is "+properties.getProperty("foo1"));
    }
}
XML

数据如下: sample.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="foo1">data1</entry>
<entry key="foo0">data0</entry>
</properties>

解析如下:

package com.argcandargv.sample;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class ParseXML{
    public static void main(String []args) throws IOException{
        String fileName="/home/yu/KEG/test/java/PropertiesUsage/sample.xml";
        Properties properties = new Properties();
        FileInputStream fileInpustStream = new FileInputStream(fileName);
        properties.loadFromXML(fileInpustStream);//
        fileInpustStream.close();
        properties.list(System.out);
        System.out.println("properties of foo1 is "+properties.getProperty("foo1"));
    }
}

两者输出是一样的,如下:

-- listing properties --
foo1=data1
foo0=data0
properties of foo1 is data1

3. 写:

.properties

对上面数据进行添加数据和保存

package com.argcandargv.sample;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class ParseProperties{
    public static void main(String []args) throws IOException{
        String fileName="/home/yu/KEG/test/java/PropertiesUsage/sample.properties";
        Properties properties = new Properties();
        FileInputStream fileInpustStream = new FileInputStream(fileName);
        properties.load(fileInpustStream);
        FileOutputStream fileOutputStream = new FileOutputStream(fileName);
        properties.setProperty("foo2", "data2");
        properties.store(fileOutputStream, "add foo2");
        fileInpustStream.close();
        fileOutputStream.close();
    }
}

被修改后的sample.properties如下:

#add foo2
#Mon Jan 21 11:02:12 CST 2013
foo2=data2
foo1=data1
foo0=data0
XML
package com.argcandargv.sample;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class ParseXML{
    public static void main(String []args) throws IOException{
        String fileName="/home/yu/KEG/test/java/PropertiesUsage/sample.xml";
        Properties properties = new Properties();
        FileInputStream fileInpustStream = new FileInputStream(fileName);
        properties.loadFromXML(fileInpustStream);
        properties.setProperty("foo2", "data2");
        FileOutputStream fileOutputStream = new FileOutputStream(fileName);
        properties.storeToXML(fileOutputStream, "add foo2");
        fileInpustStream.close();
        fileOutputStream.close();
    }
}

保存结果(sample.xml)

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>add foo2</comment>
<entry key="foo2">data2</entry>
<entry key="foo1">data1</entry>
<entry key="foo0">data0</entry>
</properties>

4. 注意事项

1' properties是无顺序的 2' new完一个Properties后,完全可以load任何数据,自行设置properties 3' properties是hash的,所以key是唯一的 4' xml和.properties不过是保存方式不同而已,选择自己喜欢的即可,它们之间的优劣,恐怕只在审美上面 5' 可以注意到,xml的store还可以设置encoding,这些小问题完全可以查看API灵活选择自己需要的东西

5. References:

[1] java.util.Properties类 学习笔记 : http://trans.blog.51cto.com/503170/110227/

[2] java.util.properties 类的使用 : http://blog.csdn.net/cece_2012/article/details/7522964


Yu

Ideals are like the stars: we never reach them, but like the mariners of the sea, we chart our course by them.

Leave a Reply

Your email address will not be published.