返回值为空,读不到配置文件
public static String read(String fileName,String key) {
FileInputStream inputFile = null;
String filePath = null;
Properties pro = new Properties();
try {
filePath = new String(URLDecoder.decode(PropertyUtil.class
.getClassLoader().getResource(fileName).getPath(),
"UTF-8"));
} catch (Unsupported
EncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("-----------"+filePath);
try {
String value = "";
inputFile = new FileInputStream(filePath);
pro.load(inputFile);
inputFile.close();
if(pro.contains(key)){
value = pro.getProperty(key);
return value;
}else{
return "";
}
} catch (
FileNotFoundException e) {
e.printStackTrace();
return "";
} catch (
IOException e) {
e.printStackTrace();
return "";
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
求大虾们帮忙看看那。。。
------解决方案--------------------不小心看错了 应该是
System.out.println(new File(fileName).getAbsolutePath());
------解决方案--------------------ResourceBundle bundle = ResourceBunder.getBundle("context-adms");
String ipLibPath = bundle.getString("adms.iplib.path");
------解决方案--------------------
package com.accp.test;
import java.io.File;
import java.io.FileInputStream;
import
java.io.FileNotFoundException;
import
java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertyUtil {
public static void main(String[] args){
System.out.println(read("aa.properties","aa"));
}
public static String read(String fileName, String key) {
InputStream inputFile = null;
String filePath = null;
Properties pro = new Properties();
try {
filePath = PropertyUtil.class.getResource(fileName).getPath();
System.out.println("-----------" + filePath);
inputFile = new FileInputStream(new File(PropertyUtil.class.getResource(fileName).getPath()));
} catch (Exception e1) {
e1.printStackTrace();
}
try {
String value = "";
pro.load(inputFile);
inputFile.close();
if (pro.containsKey(key)) {
value = pro.getProperty(key);
return value;
} else {
return "";
}
} catch (File
NotFoundException e) {
e.printStackTrace();
return "";
} catch (IOException e) {
e.printStackTrace();
return "";
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
}