PropertiesUtil 读取配置文件工具类

 1 package org.konghao.basic.util;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.FileNotFoundException;
 5 import java.io.IOException;
 6 import java.io.InputStream;
 7 import java.util.Enumeration;
 8 import java.util.HashMap;
 9 import java.util.Map;
10 import java.util.Map.Entry;
11 import java.util.Properties;
12 
13 import org.apache.wicket.util.file.File;
14 import org.junit.Test;
15 
16 public class PropertiesUtil {
17     
18     private static Properties properties;
19     
20     private String proper_resource;
21     public void setProper_resource(String proper_resource) {
22         this.proper_resource = proper_resource;
23     }
24     
25     public PropertiesUtil(String proper_resource) {
26         this.proper_resource = proper_resource;
27     }
28 
29     public Map<String,String> getProperties(){
30         properties = getInstance();
31         try {
32             InputStream inputStream = PropertiesUtil.class.getClassLoader().getResourceAsStream(proper_resource);
33             properties.load(inputStream);
34         } catch (IOException e) {
35             e.printStackTrace();
36         }
37         Map<String,String> regMap = new HashMap<String, String>();
38         regMap.put("appId", properties.getProperty("appId"));
39         regMap.put("appsecret", properties.getProperty("appsecret"));
40         regMap.put("base_url", properties.getProperty("base_url"));
41         regMap.put("weixin_token", properties.getProperty("weixin_token"));
42         return regMap;
43     }
44     
45     public static Properties getInstance(){
46         if(null == properties){
47             properties = new Properties();
48         }
49         return properties;
50     }
51     
52     public static Properties loadResource(String file){
53         properties = getInstance();
54         try {
55             InputStream inputStream = PropertiesUtil.class.getClassLoader().getResourceAsStream(file);
56             System.out.println(inputStream);
57             if(properties != null){
58                 properties.load(inputStream);
59             }
60         } catch (IOException e) {
61             e.printStackTrace();
62         }
63         return properties;
64     }
65     
66     public static void main(String[] args) {
67         PropertiesUtil pu = new PropertiesUtil("weixin_basic.properties");
68         Map<String, String> propsMap = pu.getProperties();
69         
70         for(Entry<String, String> entry : propsMap.entrySet()) {
71             System.out.println(("key: " + entry.getKey() + ", value: " + entry.getValue()));
72         }
73         
74         Properties properties = loadResource("weixin_basic.properties");
75         System.out.println(properties.getProperty("appId"));
76     }
77 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值