读书人

获取配置文件的几种模式

发布时间: 2012-12-22 12:05:06 作者: rapoo

获取配置文件的几种方式
public class PropertyUtil {

/**
* 得到Properties
*
* @param path
* @return
*/
public static Properties getProperties(String path) {
if (path == null || path.equals(""))
return null;
Properties prop = new Properties();
InputStream in;
try {
in = PropertyUtil.class.getClassLoader().getResourceAsStream(path);
prop.load(in);
} catch (Exception e1) {
e1.printStackTrace();
}

return prop;
}

/**
* 得到给出键的属性值
*
* @param path
* @param key
* @return
*/
public static String getProperty(String path, String key) {
if (path == null || path.equals("") || key == null || key.equals(""))
return null;
Properties prop = new Properties();
InputStream resource = System.class.getResourceAsStream(path);
try {
prop.load(resource);
} catch (IOException e) {
e.printStackTrace();
}
return prop.getProperty(key);
}

/**
* 得到给定键的属性值
*
* @param resource
* @param key
* @return
*/
public static String getProperty(InputStream resource, String key) {
if (resource == null || key == null || key.equals(""))
return null;
Properties prop = new Properties();
try {
prop.load(resource);
} catch (IOException e) {
e.printStackTrace();
}
return prop.getProperty(key);
}

}

读书人网 >编程

热点推荐