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 }
时间: 2024-07-30 03:02:36

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

java 读取配置文件工具类 (how to read values from properties file in java)

Java 读取配置文件工具类 使用 java.util.Properties import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class PropertiesReader { private static Properties prop; static { reload(); } private static void reload() { prop = new

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

这里主要介绍PropertyPlaceholderConfigurer这个类的使用,spring中的该类主要用来读取配置文件并将配置文件中的变量设置到上下文环境中,并进行赋值. 一.此处使用list标签将多properties文件信息读取到PropertyPlaceholderConfigurer类中 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.Propert

POI读取excel工具类 返回实体bean集合

本文举个简单的实例 读取上图的 excel文件到 List<User>集合 首先 导入POi 相关 jar包 在pom.xml 加入 <!-- poi --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.9</version> </dependency> &l

读取配置文件工具demo

//读取配置文件public class ResourcesUtils { /* * @description:根据属性获取文件名 * * @param:propertyName文件的属性名 * * @return:返回文件的属性值 * */ public static String getByName( String propertyName) { String resultM = "";//返回结果 ResourceBundle bundle = ResourceBundle.ge

java读取属性配置文件工具类

import java.io.IOException; import java.io.InputStream; import java.util.Properties; /** * * 类: ProUtil <br> * 描述: 属性配置文件读取类 <br> * 作者: poseidon<br> * 版本: 1.0<br> * 时间: 2015-7-17 上午09:20:17 */ public class ProUtil { /* 私有构造方法,防止被实例

solr分布式索引【实战一、分片配置读取:工具类configUtil.java,读取配置代码片段,配置实例】

1 private static Properties prop = new Properties(); 2 3 private static String confFilePath = "conf" + File.separator + "config.properties";// 配置文件目录 4 static { 5 // 加载properties 6 InputStream is = null; 7 InputStreamReader isr = null;

Java加载Properties配置文件工具类

import org.apache.commons.lang3.StringUtils; import org.apache.log4j.Logger; import java.io.InputStream; import java.util.HashMap; import java.util.Map; import java.util.Properties; /** * Created by yang on 2017/1/5. * 静态配置 */ public class Constants

使用Properties去读取配置文件,并获得具体内容值

有时候,写了一个配置文件,需要知道读出来的内容对不对,我们需要测试一下,看看读出来的跟我们要的是不是一样.这里写了一个工具类,用来读取配置文件里面的内容. 1.新建一个java工程,导入需要的jar包,新建一个配置文件 如下图: 2.配置文件的内容: 1 driver=com.mysql.jdbc.Driver 2 url=jdbc:mysql://localhost:3306/csdn 3 user=root 4 pwd=123456 5 initsize=1 6 maxactive=1 7

java学习-加载.properties工具类

javaWeb项目,要加载xxx.properties或其它如.txt, .md后缀的文本文件 文本内容有两种格式 key:value或者key=value 诸如Spring框架,Jfinal框架,都是使用java.util.Properties类来加载文本配置文件 Poperties类是按行读取key和value,所以我们写配置文件时,只能一行一个key/value键值对 这些配置文件一般会在编译时打包到WEB-INF/classes/文件夹下 我们要加载时就要通过使用下面方法获取InputS