Java 读写Properties配置文件

小鱼儿 2022-06-14 10:09 340阅读 0赞

原文来自:http://www.cnblogs.com/bakari/p/3562244.html

使用JAVA Properties还是很有帮助的。

#JAVA Properties类介绍
Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件,配置文件中很多变量是经常改变的,这样做也是为了方便用户,让用户能够脱离程序本身去修改相关的变量设置。像Python支持的配置文件是.ini文件,同样,它也有自己读取配置文件的类ConfigParse,方便程序员或用户通过该类的方法来修改.ini配置文件。在Java中,其配置文件常为.properties文件,格式为文本文件,文件的内容的格式是“键=值”的格式,文本注释信息可以用”#”来注释。

提供的方法

它提供了几个主要的方法:

1. getProperty ( String key),用指定的键在此属性列表中搜索属性。也就是通过参数 key ,得到 key 所对应的 value。

2. load ( InputStream inStream),从输入流中读取属性列表(键和元素对)。通过对指定的文件(比如说上面的 test.properties 文件)进行装载来获取该文件中的所有键 - 值对。以供 getProperty ( String key) 来搜索。

3. setProperty ( String key, String value) ,调用 Hashtable 的方法 put 。他通过调用基类的put方法来设置 键 - 值对。

4. store ( OutputStream out, String comments),以适合使用 load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。

5. clear (),清除所有装载的 键 - 值对。该方法在基类中提供。

相关实例

以下是我要操作的文本config.properties:

  1. name=JJ
  2. Weight=4444
  3. Height=3333

操作程序:

  1. package Test;
  2. import java.io.BufferedInputStream;
  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.Enumeration;
  9. import java.util.Properties;
  10. public class Test {
  11. //根据Key读取Value
  12. public static String GetValueByKey(String filePath, String key) {
  13. Properties pps = new Properties();
  14. try {
  15. InputStream in = new BufferedInputStream (new FileInputStream(filePath));
  16. pps.load(in);
  17. String value = pps.getProperty(key);
  18. System.out.println(key + " = " + value);
  19. return value;
  20. }catch (IOException e) {
  21. e.printStackTrace();
  22. return null;
  23. }
  24. }
  25. //读取Properties的全部信息
  26. public static void GetAllProperties(String filePath) throws IOException {
  27. Properties pps = new Properties();
  28. InputStream in = new BufferedInputStream(new FileInputStream(filePath));
  29. pps.load(in);
  30. Enumeration en = pps.propertyNames(); //得到配置文件的名字
  31. while(en.hasMoreElements()) {
  32. String strKey = (String) en.nextElement();
  33. String strValue = pps.getProperty(strKey);
  34. System.out.println(strKey + "=" + strValue);
  35. }
  36. }
  37. //写入Properties信息
  38. public static void WriteProperties (String filePath, String pKey, String pValue) throws IOException {
  39. Properties pps = new Properties();
  40. InputStream in = new FileInputStream(filePath);
  41. //从输入流中读取属性列表(键和元素对)
  42. pps.load(in);
  43. //调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
  44. //强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
  45. OutputStream out = new FileOutputStream(filePath);
  46. pps.setProperty(pKey, pValue);
  47. //以适合使用 load 方法加载到 Properties 表中的格式,
  48. //将此 Properties 表中的属性列表(键和元素对)写入输出流
  49. pps.store(out, "Update " + pKey + " name");
  50. }
  51. public static void main(String [] args) throws IOException{
  52. String value = GetValueByKey("data/config.properties", "name");
  53. System.out.println(value);
  54. GetAllProperties("data/config.properties");
  55. WriteProperties("data/config.properties","long", "212");
  56. }
  57. }

这里写图片描述

发表评论

表情:
评论列表 (有 0 条评论,340人围观)

还没有评论,来说两句吧...

相关阅读

    相关 JavaProperties配置文件

    Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形式来保存属性集。不过Properties有特殊的地方,就是它的键和值都是字符串类型。