java读取properties的工具类PropertiesUtil

 1 package org.properties.util;
 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.Properties;
 9 
10 
11 public class PropertiesUtil {
12     private String properiesName = "";
13 
14     public PropertiesUtil() {
15 
16     }
17     public PropertiesUtil(String fileName) {
18         this.properiesName = fileName;
19     }
20     public String readProperty(String key) {
21         String value = "";
22         InputStream is = null;
23         try {
24             is = PropertiesUtil.class.getClassLoader().getResourceAsStream(
25                     properiesName);
26             Properties p = new Properties();
27             p.load(is);
28             value = p.getProperty(key);
29         } catch (IOException e) {
30             // TODO Auto-generated catch block
31             e.printStackTrace();
32         } finally {
33             try {
34                 is.close();
35             } catch (IOException e) {
36                 // TODO Auto-generated catch block
37                 e.printStackTrace();
38             }
39         }
40         return value;
41     }
42 
43     public Properties getProperties() {
44         Properties p = new Properties();
45         InputStream is = null;
46         try {
47             is = PropertiesUtil.class.getClassLoader().getResourceAsStream(
48                     properiesName);
49             p.load(is);
50         } catch (IOException e) {
51             // TODO Auto-generated catch block
52             e.printStackTrace();
53         } finally {
54             try {
55                 is.close();
56             } catch (IOException e) {
57                 // TODO Auto-generated catch block
58                 e.printStackTrace();
59             }
60         }
61         return p;
62     }
63 
64     public void writeProperty(String key, String value) {
65         InputStream is = null;
66         OutputStream os = null;
67         Properties p = new Properties();
68         try {
69             is = new FileInputStream(properiesName);
70             p.load(is);
71             os = new FileOutputStream(PropertiesUtil.class.getClassLoader().getResource(properiesName).getFile());
72 
73             p.setProperty(key, value);
74             p.store(os, key);
75             os.flush();
76             os.close();
77         } catch (Exception e) {
78             // TODO Auto-generated catch block
79             e.printStackTrace();
80         } finally {
81             try {
82                 if (null != is)
83                     is.close();
84                 if (null != os)
85                     os.close();
86             } catch (IOException e) {
87                 // TODO Auto-generated catch block
88                 e.printStackTrace();
89             }
90         }
91 
92     }
93 
94 
95 
96 }
原文地址:https://www.cnblogs.com/geekdc/p/5234096.html