配置文件放在resources文件夹里
package com.repWater.Utils;
/**
* Created by Lucy on 2018/5/23.
*/
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.util.Properties;
/**
* Desc:properties文件获取工具类
* Created by hafiz.zhang on 2016/9/15.
*/
public class PropertyUtil {
private static final Logger logger = LoggerFactory.getLogger(PropertyUtil.class);
private static Properties props;
static{
loadProps();
}
synchronized static private void loadProps(){
props = new Properties();
InputStream in = null;
try {
//in = PropertyUtil.class.getClassLoader().getResourceAsStream("param.properties");
in = PropertyUtil.class.getResourceAsStream("/param.properties");
props.load(in);
} catch (FileNotFoundException e) {
logger.error("jdbc.properties文件未找到");
} catch (IOException e) {
logger.error("出现IOException");
} finally {
try {
if(null != in) {
in.close();
}
} catch (IOException e) {
logger.error("jdbc.properties文件流关闭出现异常");
}
}
}
public static String getProperty(String key){
if(null == props) {
loadProps();
}
return props.getProperty(key);
}
public static String getProperty(String key, String defaultValue) {
if(null == props) {
loadProps();
}
return props.getProperty(key, defaultValue);
}
}
如果是中文则会乱码,转码一下
中文乱码new String(name.getBytes("ISO8859-1"), "UTF-8")