public class ReadConf {
private Properties prop;
private FileInputStream inputFile;
private static String propsFile = "src/config/jdbc.properties"; // 初始化Configuration类
public ReadConf() {
prop = new Properties();
} // 初始化Configuration类-带参数
public ReadConf(String filePath) {
prop = new Properties();
try {
inputFile = new FileInputStream(filePath);
System.out.println(filePath);
prop.load(inputFile);
System.out.println("装载.properties文件成功");
inputFile.close();
} catch (FileNotFoundException e) {
System.out.println("文件不存在或者路径错误");
} catch (IOException e) {
e.printStackTrace();
}
} // 得到key的值
public String getValue(String key) {
String value = "";
System.out.println("查找" + key);
if (prop.contains(key)) {
System.out.println("包含" + key +"键");
value = prop.getProperty(key);
}
return value;
} public static void main(String[] args) {
ReadConf rc = new ReadConf(propsFile);
String key = "user";
System.out.println(rc.getValue(key));
}
}打印输出为空,不清楚哪里出错。