java中常量文件的配置与读取
2024-08-24 16:00:19
java中常量文件的配置与读取:
package com.floor.shop.user.util; import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties; /**
* 课程笔记:http://www.cnblogs.com/newAndHui/category/1153640.html
* 疑问咨询wx:851298348
*/
public class ConfigMapUtil {
private static Map<String, String> map = new HashMap<>(); static {
try {
//读取文件流
InputStream resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties");
//转变为字符流
InputStreamReader inputStreamReader = new InputStreamReader(resourceAsStream,"utf-8");
//创建 Properties 对象
Properties properties = new Properties();
// prop.load(new InputStreamReader(in, "utf-8"));
//加载字符流
properties.load(inputStreamReader);
//获取所有key
Enumeration enumeration = properties.propertyNames();
while (enumeration.hasMoreElements()) {
//遍历key
String key = (String) enumeration.nextElement();
//根据key取值
String value = properties.getProperty(key);
//放入map中
map.put(key, value);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static String getShopWx() {
return map.get("shop.wx");
}
public static String getValueByKey(String key) {
return map.get(key);
} public static Map<String, String> getMap() {
return map;
} public static void setMap(Map<String, String> map) {
ConfigMapUtil.map = map;
} }
3.测试:
最新文章
- DOM Element节点类型详解
- Thinking in Java——笔记(3)
- Python全栈--7模块--random os sys time datetime hashlib pickle json requests xml
- iOS NSDate计算时间间隔
- JavaScript 之 走马灯
- acrobat GetSize 返回 x,y 值单位
- android studio 报ambiguous method call
- iptables 实现centos内网机器访问外网
- shrio初体验(2)Realm
- 软工+C(2017第6期) 最近发展区/脚手架
- from bs4 import BeautifulSoup 报错
- Flask中Mysql数据库的常见操作
- vue 工作学习总结
- 27、通过visual s&#39;tudio 验证 SOCKET编程:搭建一个TCP服务器
- VS2012使用验证控件出现[ASP.NET]WebForms UnobtrusiveValidationMode 需要 &#39;jquery&#39; 的 ScriptResourceMapping。請加入 ScriptResourceMapping 命名的 jquery (區分大小寫)。的解决办法。
- TensorFlow queue多线程读取数据
- Django之集合函数使用与mysql表的创建特殊字段分析
- IDEA 的主题设置
- IIS 7.0 SSL 部署指南
- 加密算法(对称加密)AES、DES (非对称加密)RSA、DSA
热门文章
- Maven2查看源码
- XMLHttpRequest详解
- mybatis批量插入和批量更新
- Codeforces Round #545 (Div. 1)
- asp.net 的三种开发模式
- Go语言之unsafe包介绍及使用
- BZOJ2124 等差子序列(树状数组+哈希)
- Code First 重复外键
- git报错failed to push some refs to &#39;git@github.com:Markprint/github.git&#39;
- 16 利用Zabbix完成windows监控