java中常量文件的配置与读取

java中常量文件的配置与读取:

 1 package com.floor.shop.user.util;
 2 
 3 import java.io.InputStream;
 4 import java.io.InputStreamReader;
 5 import java.util.Enumeration;
 6 import java.util.HashMap;
 7 import java.util.Map;
 8 import java.util.Properties;
 9 
10 /**
11  * 课程笔记:http://www.cnblogs.com/newAndHui/category/1153640.html
12  * 疑问咨询wx:851298348
13  */
14 public class ConfigMapUtil {
15     private static Map<String, String> map = new HashMap<>();
16 
17     static {
18         try {
19             //读取文件流
20             InputStream resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties");
21             //转变为字符流
22             InputStreamReader inputStreamReader = new InputStreamReader(resourceAsStream,"utf-8");
23             //创建 Properties 对象
24             Properties properties = new Properties();
25            // prop.load(new InputStreamReader(in, "utf-8"));
26             //加载字符流
27             properties.load(inputStreamReader);
28             //获取所有key
29             Enumeration enumeration = properties.propertyNames();
30             while (enumeration.hasMoreElements()) {
31                 //遍历key
32                 String key = (String) enumeration.nextElement();
33                 //根据key取值
34                 String value = properties.getProperty(key);
35                 //放入map中
36                 map.put(key, value);
37             }
38         } catch (Exception e) {
39             e.printStackTrace();
40         }
41     }
42     public static String getShopWx() {
43         return map.get("shop.wx");
44     }
45     public static String getValueByKey(String key) {
46         return map.get(key);
47     }
48 
49     public static Map<String, String> getMap() {
50         return map;
51     }
52 
53     public static void setMap(Map<String, String> map) {
54         ConfigMapUtil.map = map;
55     }
56 
57 }
View Code

3.测试:

 

转载于:https://www.cnblogs.com/dw3306/p/9328978.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值