Java知识积累——Properties类的使用示例
2013-03-14 16:28
260 查看
最近一直在研究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文件
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties> <comment>test target xml file</comment> <entry key="content2">hello world</entry> <entry key="content1">there is no problem</entry> </properties>
需要强调的是,对于xml文件的读取,仅支持如上代码所示的格式,此格式为properties.dtd中规定的。其他规则的xml配置将不能被正确识别且会抛出异常。而对于properties文件则没有那么多要求。
相关文章推荐
- Java知识积累——Properties类的使用示例
- Java基础知识强化之IO流笔记72:NIO之 NIO核心组件(NIO使用代码示例)
- java提高篇(一)拓展篇 java知识汇总---IO流的使用规律总结(含代码示例)浅显易懂
- 【知识积累】java.util.Properties类 操作
- SQL脚本积累之三-----case....when...的使用示例
- Java学习之容器上(Collection接口常用方法,Iterator接口,使用foreach循环遍历Collection集合元素,Set集合通用知识(Hashset类,hashcode()与LinkedHashSet类))
- java system类使用方法示例 获取系统信息
- 使用java采集京东商城行政区划数据示例
- java并发中CountDownLatch的使用示例
- java:Properties类的相关知识
- Java使用示例
- Java 集合系列06之 Vector详细介绍(源码解析)和使用示例
- Java中Properties类的使用
- Java中LinkedList详解和使用示例_动力节点Java学院整理
- Java基础知识强化之IO流笔记32:转换流之OutputStreamWriter的使用
- java基础知识(三)-- String使用equals和==比较的区别
- java中Properties类的使用2
- RabbitMQ学习(二):Java使用RabbitMQ要点知识
- 黑马程序员——Java基础——IO流笔记(BufferedReader使用示例)
- 黑马程序员——Java基础——IO流笔记(PrintWriter使用示例)