java操作properties文件

Properties类的重要方法
Properties 类存在于胞 Java.util 中,该类继承自 Hashtable
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 () ,清除所有装载的 键 - 值对。该方法在基类中提供。

封装类:

methonds:

1.根据key获取value

2.读取properties中所有信息

3.写入信息

 1 import java.io.BufferedInputStream;
 2 import java.io.FileInputStream;
 3 import java.io.FileOutputStream;
 4 import java.io.IOException;
 5 import java.io.InputStream;
 6 import java.io.OutputStream;
 7 import java.util.Enumeration;
 8 import java.util.Properties;
 9 
10 public class TestMain {
11  
12  //根据key读取value
13  public static String readValue(String filePath,String key) {
14   Properties props = new Properties();
15         try {
16          InputStream in = new BufferedInputStream (new FileInputStream(filePath));
17          props.load(in);
18          String value = props.getProperty (key);
19             System.out.println(key+value);
20             return value;
21         } catch (Exception e) {
22          e.printStackTrace();
23          return null;
24         }
25  }
26  
27  //读取properties的全部信息
28     public static void readProperties(String filePath) {
29      Properties props = new Properties();
30         try {
31          InputStream in = new BufferedInputStream (new FileInputStream(filePath));
32          props.load(in);
33             Enumeration en = props.propertyNames();
34              while (en.hasMoreElements()) {
35               String key = (String) en.nextElement();
36                     String Property = props.getProperty (key);
37                     System.out.println(key+Property);
38                 }
39         } catch (Exception e) {
40          e.printStackTrace();
41         }
42     }
43 
44     //写入properties信息
45     public static void writeProperties(String filePath,String parameterName,String parameterValue) {
46      Properties prop = new Properties();
47      try {
48       InputStream fis = new FileInputStream(filePath);
49             //从输入流中读取属性列表(键和元素对)
50             prop.load(fis);
51             //调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
52             //强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
53             OutputStream fos = new FileOutputStream(filePath);
54             prop.setProperty(parameterName, parameterValue);
55             //以适合使用 load 方法加载到 Properties 表中的格式,
56             //将此 Properties 表中的属性列表(键和元素对)写入输出流
57             prop.store(fos, "Update '" + parameterName + "' value");
58         } catch (IOException e) {
59          System.err.println("Visit "+filePath+" for updating "+parameterName+" value error");
60         }
61     }
62 
63     public static void main(String[] args) {
64      readValue("info.properties","url");
65         writeProperties("info.properties","age","21");
66         readProperties("info.properties" );
67         System.out.println("OK");
68     }
原文地址:https://www.cnblogs.com/zipon/p/6006649.html