1、PropertiesUtil
package net.hjyzg.util.properties;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
/**
* 读取Properties文件信息
* @ClassName: GetProperties
* @Description: TODO(这里用一句话描述这个类的作用)
*
*/
public class PropertiesUtil {
/**
* 读取资源文件,并处理中文乱码
* @param filename
* @return
*/
public static Properties readPropertiesFileObj(String filename) {
Properties properties = new OrderedProperties();
try {
InputStream in = PropertiesUtil.class.getClassLoader()
.getResourceAsStream(filename);
BufferedReader bf = new BufferedReader(new InputStreamReader(in, "utf-8"));
properties.load(bf);
in.close(); // 关闭流
} catch (IOException e) {
e.printStackTrace();
}
return properties;
}
/**
* 获取指定properties的资源
* @param fileName
* @return
*/
public static Properties getKey(String fileName) {
Properties pro = readPropertiesFileObj(fileName);
return pro;
}
}
2、OrderedProperties
/**
* @ClassName: OrderedProperties.java
* @Description: TODO(用一句话描述该文件做什么)
*/
package net.hjyzg.util.properties;
import java.util.Collections;
import java.util.Enumeration;
import java.util.LinkedHashSet;
import java.util.Properties;
import java.util.Set;
/**
* 获取properties文件内容
* @ClassName: OrderedProperties
* @Description: TODO(这里用一句话描述这个类的作用)
*
*/
public class OrderedProperties extends Properties{
private static final long serialVersionUID = -4627607243846121965L;
private final LinkedHashSet<Object> keys = new LinkedHashSet<Object>();
public Enumeration<Object> keys() {
return Collections.<Object> enumeration(keys);
}
public Object put(Object key, Object value) {
keys.add(key);
return super.put(key, value);
}
public synchronized Object remove(Object key) {
keys.remove(key);
return super.remove(key);
}
public Set<Object> keySet() {
return keys;
}
public Set<String> stringPropertyNames() {
Set<String> set = new LinkedHashSet<String>();
for (Object key : this.keys) {
set.add((String) key);
}
return set;
}
}
3、页面调用
Properties pro = PropertiesUtil.getKey("weixin.properties");
map.put("appid", pro.getProperty("appid"));
map.put("appsecret", pro.getProperty("appsecret"));