博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java知识积累——Properties类的使用示例
阅读量:6935 次
发布时间:2019-06-27

本文共 4125 字,大约阅读时间需要 13 分钟。

最近一直在研究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
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
) 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
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
) 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 
2 3
4
test target xml file
5
hello world
6
there is no problem
7

需要强调的是,对于xml文件的读取,仅支持如上代码所示的格式,此格式为properties.dtd中规定的。其他规则的xml配置将不能被正确识别且会抛出异常。而对于properties文件则没有那么多要求。

转载于:https://www.cnblogs.com/FlameRen/archive/2013/03/14/2959586.html

你可能感兴趣的文章
我爱免费之FreeFileSync文件夹同步软件
查看>>
lufylegend:图片的加载和显示
查看>>
献给所有从事IT行业拥有梦想的英语渣们
查看>>
Linux命令-更新系统时间和硬件时间
查看>>
音频AAC编码浅析
查看>>
linux系统时间和硬件时钟问题(date和hwclock)
查看>>
你用过这种奇葩的C#注释吗?如何看待
查看>>
memcache安装
查看>>
我的第一个DMZ方案实践
查看>>
HTML5--Video
查看>>
UVM中的regmodel建模(三)
查看>>
如何查看LoadRunner虚拟用户(vuser)类型
查看>>
oracle截取字符串区间段的一部分字符串
查看>>
Ubuntu Gnome 14.04.2 lts 折腾笔记
查看>>
前台的js对象数组传到后台处理。在前台把js对象数组转化为json字符串,在后台把json字符串解析为List<>...
查看>>
HTML目录
查看>>
【Hibernate学习笔记-5】@Formula注解的使用
查看>>
java ClassLoader static
查看>>
公司交换机arp 绑定操作
查看>>
东大oj-1511: Caoshen like math
查看>>