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类的list方法,也可使用store方法。
输出后的内容如下
properties文件
xml文件
需要强调的是,对于xml文件的读取,仅支持如上代码所示的格式,此格式为properties.dtd中规定的。其他规则的xml配置将不能被正确识别且会抛出异常。而对于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文件则没有那么多要求。
相关文章推荐
- Java知识积累——Properties类的使用示例
- Java知识积累——Properties类的使用示例
- Properties类的使用示例
- 方框滤波boxFilter函数的使用示例程序
- Zebra 在SAP中的使用示例(中英文版本)
- Redis安装和使用示例
- crontab使用方法和示例
- Visitor模式示例(使用Loki类库)
- VS2008+QT中使用CUDA的示例
- 【Mybatis】mybatis使用示例
- PHP 使用header函数设置HTTP头的示例解析 表头
- JAVA中常用接口的介绍及使用示例:java.lang.Comparable
- c#定时器使用示例详解
- Java 集合系列17之 TreeSet详细介绍(源码解析)和使用示例
- 网络状态检测------苹果官方示例Reachability的使用
- 针对某个表使用高级复制进行数据同步示例
- ConcurrentHashMap使用示例
- string.Join和Reverse的简单使用示例
- velocity.js 使用示例
- php使用多个进程同时控制文件读写示例