【发布时间】:2016-03-07 13:57:37
【问题描述】:
PROJECT/resources/properties.properties 中的此属性文件可以通过以下方式读取并显示其内容:
public void showFileContent(String fileName){
File file = new File (fileName);
FileInputStream input = null;
if(file.exists()){
int content;
try {
input = new FileInputStream(fileName);
while ((content = input.read()) != -1) {
System.out.print((char) content);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if (input != null) {
try {
input.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}else{
System.out.println("Error : properties File " + fileName + " not found");
}
}
但它在properties.load 处出现空指针异常并使用该代码失败
public Properties getProperties(String fileName, Properties properties){
File file = new File (fileName);
InputStream input = null;
if(file.exists()){
try {
input = new FileInputStream(fileName);
properties.load(input);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}else{
System.out.println("Error : properties File " + fileName + " not found");
}
return properties;
}
即使输入设置为
input = this.getClass().getClassLoader().getResourceAsStream(fileName)
任何人都知道为什么这两种方法的属性文本文件的路径相同?
【问题讨论】:
标签: java