java读取properties的工具类PropertiesUtil

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 }

 

以上是关于java读取properties的工具类PropertiesUtil的主要内容,如果未能解决你的问题,请参考以下文章

Java-Properties文件读取工具类

java 读取配置文件工具类 (how to read values from properties file in java)

Java学习笔记5.4.3 Map接口 - Properties类

读取Config文件工具类 PropertiesConfig.java

java学习-加载.properties工具类

solr分布式索引实战分片配置读取:工具类configUtil.java,读取配置代码片段,配置实例