您的位置:首页 > 运维架构

Properties类的使用示例

2013-04-10 19:04 162 查看
最近一直在研究properties配置文件,突然碰到了一个java的类,名为Properties。该类继承自HashTable,提供的方法很像Map的实现类HashMap。一时间激发了我对此类的关注和研究,通过找资料和自行调试,发现该类能够在程序运行初期给我们提供帮助。通过解析前置文件(含程序需要的某些参数),获得程序运行所需的配置信息,存入Properties类中,供程序调用。

Properties类的作用就像其名字所体现的一样,主要是解决属性问题的,说白了就是此类和配置文件的关系十分暧昧。现将Properties类的常见使用场景概括如下:

1.从properties配置文件中读取数据,解析key-value对,存入Properties类中。

2.从xml配置问价那种读取数据,解析key-value对,存入Properties类中。

3.将Properties类中存储的key-value对,输出到properties配置文件中。

4.将Properties类中存储的key-value对,输出到xml配置文件中。

见面四个函数分别对应了这四种情况:

//读取properties文件的配置信息到Properties类中
public void readPropFile(){
Properties prop = new Properties();
Enumeration<String> en = null;
String key = null;

InputStream input = null;
try {
input = this.getClass().getClassLoader().getResourceAsStream("test.properties");
prop.load(input);
en = (Enumeration<String>) prop.propertyNames();
//prop.list(System.out);
while(en.hasMoreElements()){
key = en.nextElement();
System.out.println(key+"---"+prop.getProperty(key));
}
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
input.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}

//读取xml文件的配置信息到Properties类中
public void readXmlFile(){
Properties prop = new Properties();
Enumeration<String> en = null;
String key = null;

InputStream input = null;
try {
input = this.getClass().getClassLoader().getResourceAsStream("test.xml");
prop.loadFromXML(input);
en = (Enumeration<String>) prop.propertyNames();
//prop.list(System.out);
while(en.hasMoreElements()){
key = en.nextElement();
System.out.println(key+"---"+prop.getProperty(key));
}
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
input.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}

//将Properties类中的key-value对,存到properties文件中
public void printProp(){
Properties prop = new Properties();
prop.setProperty("content1", "there is no problem");
prop.setProperty("content2", "hello world");

//PrintWriter outputFile = null;
FileOutputStream outputFile = null;
try {
//outputFile = new PrintWriter(new File("src/target.properties"));
outputFile = new FileOutputStream("src/target.properties");
prop.store(outputFile, "test target file");
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
outputFile.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}

//将Properties类中的key-value对,存到xml文件中
public void printXML(){
Properties prop = new Properties();
prop.setProperty("content1", "there is no problem");
prop.setProperty("content2", "hello world");

//PrintWriter outputFile = null;
FileOutputStream outputFile = null;
try {
//outputFile = new PrintWriter(new File("src/target.properties"));
outputFile = new FileOutputStream("src/target.xml");
prop.storeToXML(outputFile, "test target xml file");
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
outputFile.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}


输出到文件可使用Properties类的list方法,也可使用store方法。

输出后的内容如下

properties文件

#test target file
#Thu Mar 14 15:59:01 CST 2013
content2=hello world
content1=there is no problem


xml文件



1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
3 <properties>
4 <comment>test target xml file</comment>
5 <entry key="content2">hello world</entry>
6 <entry key="content1">there is no problem</entry>
7 </properties>




需要强调的是,对于xml文件的读取,仅支持如上代码所示的格式,此格式为properties.dtd中规定的。其他规则的xml配置将不能被正确识别且会抛出异常。而对于properties文件则没有那么多要求。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: