zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    枚举 + 进制转换 --- hdu 4937 Lucky Number
    扫描线 + 线段树 : 求矩形面积的并 ---- hnu : 12884 Area Coverage
    暴力枚举 + 24点 --- hnu : Cracking the Safe
    dp or 贪心 --- hdu : Road Trip
    数论
    模拟 --- hdu 12878 : Fun With Fractions
    图论 --- spfa + 链式向前星 : 判断是否存在正权回路 poj 1860 : Currency Exchange
    图论 --- spfa + 链式向前星 (模板题) dlut 1218 : 奇奇与变形金刚
    图论 --- 最小生成树 + 剪枝 + 路径合并
    图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party
  • 原文地址:https://www.cnblogs.com/a757956132/p/5371131.html
Copyright © 2011-2022 走看看