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"));
}
}
04-14 10:42