PropertiesUtil 读取配置文件工具类
package org.konghao.basic.util; import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties; import org.apache.wicket.util.file.File;
import org.junit.Test; public class PropertiesUtil { private static Properties properties; private String proper_resource;
public void setProper_resource(String proper_resource) {
this.proper_resource = proper_resource;
} public PropertiesUtil(String proper_resource) {
this.proper_resource = proper_resource;
} public Map<String,String> getProperties(){
properties = getInstance();
try {
InputStream inputStream = PropertiesUtil.class.getClassLoader().getResourceAsStream(proper_resource);
properties.load(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
Map<String,String> regMap = new HashMap<String, String>();
regMap.put("appId", properties.getProperty("appId"));
regMap.put("appsecret", properties.getProperty("appsecret"));
regMap.put("base_url", properties.getProperty("base_url"));
regMap.put("weixin_token", properties.getProperty("weixin_token"));
return regMap;
} public static Properties getInstance(){
if(null == properties){
properties = new Properties();
}
return properties;
} public static Properties loadResource(String file){
properties = getInstance();
try {
InputStream inputStream = PropertiesUtil.class.getClassLoader().getResourceAsStream(file);
System.out.println(inputStream);
if(properties != null){
properties.load(inputStream);
}
} catch (IOException e) {
e.printStackTrace();
}
return properties;
} public static void main(String[] args) {
PropertiesUtil pu = new PropertiesUtil("weixin_basic.properties");
Map<String, String> propsMap = pu.getProperties(); for(Entry<String, String> entry : propsMap.entrySet()) {
System.out.println(("key: " + entry.getKey() + ", value: " + entry.getValue()));
} Properties properties = loadResource("weixin_basic.properties");
System.out.println(properties.getProperty("appId"));
}
}
PropertiesUtil 读取配置文件工具类的更多相关文章
- ConfigUtil读取配置文件工具类
ConfigUtil package com.sso.util; import java.io.FileNotFoundException; import java.io.IOException; i ...
- Java读取properties配置文件工具类
1. PropertyUtils.java package javax.utils; import java.io.InputStream; import java.util.Properties ...
- Java加载Properties配置文件工具类
Java加载Properties配置文件工具类 import org.apache.commons.lang3.StringUtils; import org.apache.log4j.Logger; ...
- CSharp读取配置文件的类(简单实现)
Reinventing the wheel 系列 CSharp 读取配置文件的类 简单实现(注意没有写) 本人对CS 不是很熟,库也不熟,所以到网上找个实现,并自己添加了点异常.如果只是读取信息,足够 ...
- POI读取excel工具类 返回实体bean集合(xls,xlsx通用)
本文举个简单的实例 读取上图的 excel文件到 List<User>集合 首先 导入POi 相关 jar包 在pom.xml 加入 <!-- poi --> <depe ...
- Asp.NetCore 读取配置文件帮助类
/// <summary> /// 读取配置文件信息 /// </summary> public class ConfigExtensions { public static ...
- spring读取配置文件PropertyPlaceholderConfigurer类的使用
这里主要介绍PropertyPlaceholderConfigurer这个类的使用,spring中的该类主要用来读取配置文件并将配置文件中的变量设置到上下文环境中,并进行赋值. 一.此处使用list标 ...
- 读取配置文件工具demo
//读取配置文件public class ResourcesUtils { /* * @description:根据属性获取文件名 * * @param:propertyName文件的属性名 * * ...
- 开发读取.properties 配置文件工具类PropertiesUtil
import java.io.IOException; import java.io.InputStream; import java.util.Properties; import org.juni ...
随机推荐
- pg 匹配中文字符
用到了正则表达式: 字段 ~'[\u4E00-\u9FA5]+$'; 注意:此表达式可能还不能取到最全的值.
- java-No exception of type ConfigurationException can be thrown; an exception type must be a subclass of Throwable
功能:读配置文件 java菜鸟:导入工程在报名处就开始报错,第一次遇到 import org.apache.commons.lang3.StringUtils; import org.apache.c ...
- Android高手进阶教程(五)之----Android 中LayoutInflater的使用!
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://weizhulin.blog.51cto.com/1556324/311450 大 ...
- C# 对Excel文档打印时的页面设置
1.对打印页面的朝向,页宽,页高进行设置 参考源码[1] using Excel = Microsoft.Office.Interop.Excel; Excel.Application tmpExce ...
- php codeigniter (CI) oracle 数据库配置-宋正河整理
database.php 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 $active_group = 'default'; $active_record ...
- Android百度地图开发(三)范围搜索
// 1.新建项目 将地图API添加进classpath中: 2.在activity_main.xml中添加一个MapView,用来显示地图: <LinearLayout xmlns:andro ...
- IoC(控制反转)
在传统的编程中,我们通过内部代码来控制组件之间的关系,但是这种实现方式,容易造成组件之间的高耦合.IoC能够很好地解决这个问题,它将组件间的关系从程序内部上提到外部容器来管理.IoC的核心目标是通过简 ...
- tcpdump dns包(linux高性能编程读书笔记2)
tcpdump -i eth0 -nt -s 500 port domain host -t A www.baidu.com www.baidu.com is an alias for www.a ...
- linq数据使用
取出数据库满足条件的记录的ID,把值放到list中 ) { int userid = Convert.ToInt32(Request.Cookies["id"].Value); v ...
- JavaScript基础知识整理(1)数组
第一:创建. 1,var arr= new Array(); //数组为空.长度为0. arr[0]="apple"; arr[1]="orange"; arr ...