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. }

PropertiesUtil 读取配置文件工具类的更多相关文章

  1. ConfigUtil读取配置文件工具类

    ConfigUtil package com.sso.util; import java.io.FileNotFoundException; import java.io.IOException; i ...

  2. Java读取properties配置文件工具类

    1.   PropertyUtils.java package javax.utils; import java.io.InputStream; import java.util.Properties ...

  3. Java加载Properties配置文件工具类

    Java加载Properties配置文件工具类 import org.apache.commons.lang3.StringUtils; import org.apache.log4j.Logger; ...

  4. CSharp读取配置文件的类(简单实现)

    Reinventing the wheel 系列 CSharp 读取配置文件的类 简单实现(注意没有写) 本人对CS 不是很熟,库也不熟,所以到网上找个实现,并自己添加了点异常.如果只是读取信息,足够 ...

  5. POI读取excel工具类 返回实体bean集合(xls,xlsx通用)

    本文举个简单的实例 读取上图的 excel文件到 List<User>集合 首先 导入POi 相关 jar包 在pom.xml 加入 <!-- poi --> <depe ...

  6. Asp.NetCore 读取配置文件帮助类

    /// <summary> /// 读取配置文件信息 /// </summary> public class ConfigExtensions { public static ...

  7. spring读取配置文件PropertyPlaceholderConfigurer类的使用

    这里主要介绍PropertyPlaceholderConfigurer这个类的使用,spring中的该类主要用来读取配置文件并将配置文件中的变量设置到上下文环境中,并进行赋值. 一.此处使用list标 ...

  8. 读取配置文件工具demo

    //读取配置文件public class ResourcesUtils { /* * @description:根据属性获取文件名 * * @param:propertyName文件的属性名 * * ...

  9. 开发读取.properties 配置文件工具类PropertiesUtil

    import java.io.IOException; import java.io.InputStream; import java.util.Properties; import org.juni ...

随机推荐

  1. mysql添加用户和用户权限

    Mysql添加用户使用可以对mysql数据库用户表有操作权限的用户名登陆mysqlinsert into user(Host,User,Password) values('%','name','pas ...

  2. 分享15款为jQuery Mobile定制的插件

    jQuery Mobile 1.0 已经发布了, 做为jQuery的移动设备类库, 并且依靠着jQuery这个及其受欢迎的类库,必将带给大家不一样的使用体验,基于jQuery Mobile的插件开发必 ...

  3. 44、自定义仿IOS对话框

    private Dialog myDialog = null; private void ExitLogin() { View view = LayoutInflater.from(MainActiv ...

  4. Loadrunner 性能指标定位系统瓶颈

    判断CPU瓶颈 1, %processor time 平均值大于95 2, processor queue length大于2 (大于处理器个数+1).可以确定CPU瓶颈 3, CPU空闲时间为零(z ...

  5. JSON 教程学习进度备忘

    书签:跳过:另外跳过的内容有待跟进 __________________ 学习资源:W3School. _________________ 跳过的内容: 1. ______________ 知识点:1 ...

  6. 1005acm罚时

    ACM国际大学生程序设计竞赛是由国际计算机学会主办的,一项旨在展示大学生创新能力.团队精神和在压力下编写程序.分析和解决问题能力的年度竞赛.参赛队伍最多由三名参赛队员组成,竞赛中一般命题10-13题, ...

  7. CentOS下安装R

    R的Windows版本有直接的安装包,直接下载安装很方便,但是对于CentOS6以上,不能直接通过yum 安装R,需要自己编译. 1. 在编译之前,用yum安装各种软件 (1)安装gcc > y ...

  8. 字符串和数组中split().toString(),join(),splice(),slice(),substr()和substring()

    <!Doctype html> <head> <mate charset="utf-8"> <title>string change ...

  9. String内存陷阱简介

    String 方法用于文本分析及大量字符串处理时会对内存性能造成一些影响.可能导致内存占用太大甚至OOM. 一.先介绍一下String对象的内存占用 一般而言,Java 对象在虚拟机的结构如下:•对象 ...

  10. ipc.Client: Retrying connect to server: h1/192.168.1.61:9000. Already tried 0 time(s);解决方法

    1.检查namenode服务器的是否运行正常,我的问题是没有开启hadoop集群出现的. 2.检查namenode服务器的防火墙是否开放的响应端口,一般内网建议关闭.