package com.chb.abc.common.util;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
public class PropertiesUtil {
static Map<String, Map> oMap = new HashMap<String, Map>();
public static Map getProperties(String path) {
Properties prop = new Properties();
Map<String, String> map = new HashMap<String, String>();
if (path == null) {
return null;
}
if (!path.substring(0, 1).equals("/")) {
path = "/" + path;
}
if (oMap.get(path) != null) {
return oMap.get(path);
}
InputStream in = Object.class.getResourceAsStream(path);
try {
prop.load(in);
Iterator<Entry<Object, Object>> it = prop.entrySet().iterator();
while (it.hasNext()) {
Entry<Object, Object> entry = it.next();
map.put(entry.getKey().toString(), entry.getValue().toString());
}
} catch (IOException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
return null;
}
System.out.println("-----------------------");
oMap.put(path, map);
return map;
}
public static void main(String[] args) {
PropertiesUtil.getProperties("msgConfig.properties");
PropertiesUtil.getProperties("msgConfig.properties");
}
}
<pre name="code" class="java">package com.chb.abc.common.util;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* properties文件工具类
*/
public class PropertiesUtil {
public static Properties getProperty(String path){
Properties pro = new Properties();
InputStream input =PropertiesUtil.class.getResourceAsStream(path);
try {
pro.load(input);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
return pro;
}
public static String getString(String key,String path){
if(getProperty(path).containsKey(key)){
return getProperty(path).getProperty(key);
}
return null;
}
public static int getInt(String key,String path){
try {
String s = getString(key, path);
if(s!=null){
return Integer.parseInt(s);
}
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
}