Java知识积累——Properties类的使用示例

来源:互联网 发布:百度产品经理待遇 知乎 编辑:程序博客网 时间:2024/06/12 21:24

转自:http://www.cnblogs.com/FlameRen/archive/2013/03/14/2959586.html

最近一直在研究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配置文件中。

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

复制代码
 1 //读取properties文件的配置信息到Properties类中 2 public void readPropFile(){ 3     Properties prop = new Properties(); 4     Enumeration<String> en = null; 5     String key = null; 6      7     InputStream input = null; 8     try { 9         input = this.getClass().getClassLoader().getResourceAsStream("test.properties");10         prop.load(input);11         en = (Enumeration<String>) prop.propertyNames();12         //prop.list(System.out);13         while(en.hasMoreElements()){14             key = en.nextElement();15             System.out.println(key+"---"+prop.getProperty(key));16         }17     } catch (Exception e) {18         e.printStackTrace();19     } finally{20         try {21             input.close();22         } catch (Exception e2) {23             e2.printStackTrace();24         }25     }26 }27 28 //读取xml文件的配置信息到Properties类中29 public void readXmlFile(){30     Properties prop = new Properties();31     Enumeration<String> en = null;32     String key = null;33     34     InputStream input = null;35     try {36         input = this.getClass().getClassLoader().getResourceAsStream("test.xml");37         prop.loadFromXML(input);38         en = (Enumeration<String>) prop.propertyNames();39         //prop.list(System.out);40         while(en.hasMoreElements()){41             key = en.nextElement();42             System.out.println(key+"---"+prop.getProperty(key));43         }44     } catch (Exception e) {45         e.printStackTrace();46     } finally{47         try {48             input.close();49         } catch (Exception e2) {50             e2.printStackTrace();51         }52     }53 }54 55 //将Properties类中的key-value对,存到properties文件中56 public void printProp(){57     Properties prop = new Properties();58     prop.setProperty("content1", "there is no problem");59     prop.setProperty("content2", "hello world");60     61     //PrintWriter outputFile = null;62     FileOutputStream outputFile = null;63     try {64         //outputFile = new PrintWriter(new File("src/target.properties"));65         outputFile = new FileOutputStream("src/target.properties");66         prop.store(outputFile, "test target file");67     } catch (Exception e) {68         e.printStackTrace();69     } finally{70         try {71             outputFile.close();72         } catch (Exception e2) {73             e2.printStackTrace();74         }75     }76 }77 78 //将Properties类中的key-value对,存到xml文件中79 public void printXML(){80     Properties prop = new Properties();81     prop.setProperty("content1", "there is no problem");82     prop.setProperty("content2", "hello world");83     84     //PrintWriter outputFile = null;85     FileOutputStream outputFile = null;86     try {87         //outputFile = new PrintWriter(new File("src/target.properties"));88         outputFile = new FileOutputStream("src/target.xml");89         prop.storeToXML(outputFile, "test target xml file");90     } catch (Exception e) {91         e.printStackTrace();92     } finally{93         try {94             outputFile.close();95         } catch (Exception e2) {96             e2.printStackTrace();97         }98     }99 }
复制代码

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

输出后的内容如下

properties文件

#test target file#Thu Mar 14 15:59:01 CST 2013content2=hello worldcontent1=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文件则没有那么多要求。