【Java编程】写入、读取、遍历Properties文件 ゝ一世哀愁。 2022-08-27 12:56 176阅读 0赞 转自:http://blog.csdn.net/wyply115 在Java开发中通常我们会存储配置参数信息到属性文件,这样的属性文件可以是拥有键值对的属性文件,也可以是XML文件,关于XML文件的操作,请参考博文[【Java编程】DOM XML Parser 解析、遍历、创建XML][Java_DOM XML Parser _XML]。在该篇博文中,我将展示如何向属性文件写入键值对,如何读取属性文件中的键值对,如何遍历属性文件。 ### 1、向属性文件中写入键值对 ### ![Center][] ***特别注意:*** Properties类调用setProperty方法将键值对保存到内存中,此时可以通过getProperty方法读取,propertyNames()方法进行遍历,但是并没有将键值对持久化到属性文件中,故需要*调用store()方法持久化键值对到属性文件中*,这里的store方法*类似于Android SharedPreferences的commit()方法*。 **\[java\]** [view plain][] [copy][view plain] [![在CODE上查看代码片][CODE]][CODE_CODE] [![派生到我的代码片][ico_fork.svg]][ico_fork.svg 1] 1. **package** com.andieguo.propertiesdemo; 2. 3. **import** java.io.FileInputStream; 4. **import** java.io.FileOutputStream; 5. **import** java.io.IOException; 6. **import** java.io.InputStream; 7. **import** java.io.OutputStream; 8. **import** java.util.Date; 9. **import** java.util.Enumeration; 10. **import** java.util.Properties; 11. 12. **import** junit.framework.TestCase; 13. 14. **public** **class** PropertiesTester **extends** TestCase \{ 15. 16. **public** **void** writeProperties() \{ 17. Properties properties = **new** Properties(); 18. OutputStream output = **null**; 19. **try** \{ 20. output = **new** FileOutputStream("config.properties"); 21. properties.setProperty("url", "jdbc:mysql://localhost:3306/"); 22. properties.setProperty("username", "root"); 23. properties.setProperty("password", "root"); 24. properties.setProperty("database", "bbs");//保存键值对到内存 25. properties.store(output, "andieguo modify" + **new** Date().toString());// 保存键值对到文件中 26. \} **catch** (IOException io) \{ 27. io.printStackTrace(); 28. \} **finally** \{ 29. **if** (output != **null**) \{ 30. **try** \{ 31. output.close(); 32. \} **catch** (IOException e) \{ 33. e.printStackTrace(); 34. \} 35. \} 36. \} 37. \} 38. \} 执行单元测试后,属性文件内容如下: ![SouthEast][] ### 2、读取属性文件中的键值对 ### ![Center 1][] **\[java\]** [view plain][] [copy][view plain] [![在CODE上查看代码片][CODE]][CODE_CODE] [![派生到我的代码片][ico_fork.svg]][ico_fork.svg 1] 1. **public** **class** PropertiesTester **extends** TestCase \{ 2. 3. **public** **void** loadProperties() \{ 4. Properties properties = **new** Properties(); 5. InputStream input = **null**; 6. **try** \{ 7. input = **new** FileInputStream("config.properties");//加载Java项目根路径下的配置文件 8. properties.load(input);// 加载属性文件 9. System.out.println("url:" + properties.getProperty("url")); 10. System.out.println("username:" + properties.getProperty("username")); 11. System.out.println("password:" + properties.getProperty("password")); 12. System.out.println("database:" + properties.getProperty("database")); 13. \} **catch** (IOException io) \{ 14. \} **finally** \{ 15. **if** (input != **null**) \{ 16. **try** \{ 17. input.close(); 18. \} **catch** (IOException e) \{ 19. e.printStackTrace(); 20. \} 21. \} 22. \} 23. \} 24. \} 执行单元测试方法,console输出的output如下: ![SouthEast 1][] ### 3、遍历属性文件中的键值对 ### **\[java\]** [view plain][] [copy][view plain] [![在CODE上查看代码片][CODE]][CODE_CODE] [![派生到我的代码片][ico_fork.svg]][ico_fork.svg 1] 1. **package** com.andieguo.propertiesdemo; 2. 3. **import** java.io.IOException; 4. **import** java.io.InputStream; 5. **import** java.util.Enumeration; 6. **import** java.util.Map.Entry; 7. **import** java.util.Properties; 8. **import** java.util.Set; 9. 10. **import** junit.framework.TestCase; 11. 12. **public** **class** PropertiesTester **extends** TestCase \{ 13. 14. **public** **void** printAll() \{ 15. Properties prop = **new** Properties(); 16. InputStream input = **null**; 17. **try** \{ 18. String filename = "config.properties"; 19. input = getClass().getClassLoader().getResourceAsStream(filename); 20. **if** (input == **null**) \{ 21. System.out.println("Sorry, unable to find " + filename); 22. **return**; 23. \} 24. prop.load(input); 25. //方法一: 26. Set<Object> keys = prop.keySet();//返回属性key的集合 27. **for**(Object key:keys)\{ 28. System.out.println("key:"\+key.toString()+",value:"\+prop.get(key)); 29. \} 30. //方法二: 31. Set<Entry<Object, Object>> entrys = prop.entrySet();//返回的属性键值对实体 32. **for**(Entry<Object, Object> entry:entrys)\{ 33. System.out.println("key:"\+entry.getKey()+",value:"\+entry.getValue()); 34. \} 35. //方法三: 36. Enumeration<?> e = prop.propertyNames(); 37. **while** (e.hasMoreElements()) \{ 38. String key = (String) e.nextElement(); 39. String value = prop.getProperty(key); 40. System.out.println("Key:" + key + ",Value:" + value); 41. \} 42. \} **catch** (IOException ex) \{ 43. ex.printStackTrace(); 44. \} **finally** \{ 45. **if** (input != **null**) \{ 46. **try** \{ 47. input.close(); 48. \} **catch** (IOException e) \{ 49. e.printStackTrace(); 50. \} 51. \} 52. \} 53. \} 54. 55. \} ### 4、其他方法 ### **public void list(PrintStream out)** 将属性列表输出到指定的输出流。此方法对调试很有用。 **public void storeToXML(OutputStream os,Stringcomment) throws IOException** 发出一个表示此表中包含的所有属性的 XML 文档。 ### 5、参考 ### [Java Properties File Examples][](推荐) [Java Property File example with write, read, load from Classpath and property xml file][Java Property File example with write_ read_ load from Classpath and property xml file] [Java - The Properties Class][] ### 6、你可能感兴趣的文章 ### [【Java编程】DOM XML Parser解析、遍历、创建XML][Java_DOM XML Parser _XML] [【Java编程】SAX XML Parser解析、生成XML文件][Java_SAX XML Parser_XML] [Java_DOM XML Parser _XML]: http://blog.csdn.net/andie_guo/article/details/24844351 [Center]: /images/20220824/b4aeccb52fe04165b38a2a5c143c177a.png [view plain]: http://blog.csdn.net/andie_guo/article/details/25425403# [CODE]: https://code.csdn.net/assets/CODE_ico.png [CODE_CODE]: https://code.csdn.net/snippets/336835 [ico_fork.svg]: https://code.csdn.net/assets/ico_fork.svg [ico_fork.svg 1]: https://code.csdn.net/snippets/336835/fork [SouthEast]: /images/20220824/ab5c61dd52d8430495674564323f184d.png [Center 1]: /images/20220824/95722e4a3dab4710b709e8d3475b99b3.png [SouthEast 1]: /images/20220824/618999a573fe4b9d9cb1da75c7eecac3.png [Java Properties File Examples]: http://www.mkyong.com/java/java-properties-file-examples/ [Java Property File example with write_ read_ load from Classpath and property xml file]: http://www.journaldev.com/712/java-property-file-example-with-write-read-load-from-classpath-and-property-xml-file [Java - The Properties Class]: http://www.tutorialspoint.com/java/java_properties_class.htm [Java_SAX XML Parser_XML]: http://blog.csdn.net/andie_guo/article/details/24851077
还没有评论,来说两句吧...