您的位置:首页 > 编程语言 > Java开发

Java读取Properties文件工具类

2016-04-13 16:05 621 查看
读取properties文件工具类
package zmx.util;

import java.io.InputStream;
import java.util.Properties;
/**
* 从类路径下获取资源文件并进行读写
* @author zhangwenchao
*
*/
public class PropertyUtil {

private static Properties prop = null;

/**
* 初始化Properties实例
* @param propertyName
* @throws Exception
*/
public synchronized static void initProperty(String propertyName) throws Exception {
if (prop == null) {
prop = new Properties();
InputStream inputstream = null;
ClassLoader cl = PropertyUtil.class.getClassLoader();
System.out.println(cl);
if  (cl !=  null ) {
inputstream = cl.getResourceAsStream( propertyName );
}  else {
inputstream = ClassLoader.getSystemResourceAsStream(propertyName );
}

if (inputstream == null) {
throw new Exception("inputstream " + propertyName+ " open null");
}
prop.load(inputstream);
inputstream.close();
inputstream = null;
}
}
/**
* 读取数据
* @param propertyName
* @param key
* @return
*/
public static String getValueByKey(String propertyName, String key) {
String result = "";
try {
initProperty(propertyName);
result = prop.getProperty(key);
return result;
} catch (Exception e) {
e.printStackTrace();
return "";
}
}

public static void main(String[] s) {

try {

System.out.println(PropertyUtil.getValueByKey("config_zh_CN.properties","host"));

} catch (Exception e) {

e.printStackTrace();
}

}
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: