public static String getConfigKey(String key) {
String fName = "aa.properties";
String keyVal = "";
Properties props = null;
String url = StringUtils.class.getClassLoader().getResource("/").getPath()+ fName;
/*从根目录读取配置文件
String fileName = "";
if(File.separatorChar == '\\')
fileName = "C:\\"+fName;
else
fileName = "/"+fName;
*/
//log.info("配置文件地址----------"+url);
File file = new File(url);
if (!file.isFile()) {
return null;
}
if (file.exists()) {
FileInputStream fis = null;
try
{
fis = new FileInputStream(file);
}
catch(FileNotFoundException e2)
{
log.info(file + "读取配置文件错误!");
e2.printStackTrace();
}
props = new Properties();
try
{
props.load(fis);
fis.close();
keyVal = props.getProperty(key);
if (keyVal == null) {
log.error("配置文件中没有名为:" + key + "的KEY值!");
}
if (keyVal != null && keyVal.endsWith("\"")){
keyVal = keyVal.replaceAll("\"", "");
}
}
catch(IOException e)
{
e.printStackTrace();
log.info(file + "读取配置文件中属性值错误!");
}
} else {
log.info(file + "配置文件没有找到!");
}
return keyVal;
}
java 读取工程下classes配置文件
最新推荐文章于 2021-02-28 02:40:03 发布