1
這種特性在PROJECT/ressources文件/ properties.properties可以讀取並顯示它的內容與的FileInputStream失敗的屬性文件
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一個空指針異常failes與代碼
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)
的人都知道爲什麼,可以是FO在兩個方法的相同路徑中使用屬性文本文件?
其實你是對的。我只是不明白爲什麼我不能用新的屬性擴展屬性,認爲它是一個無盡的哈希表! 實際上想要像web.properties,db屬性等文件中的屬性。 – user3732793
我們不需要擴展屬性。我們可以在任何現有的屬性對象上調用'putAll'方法來添加新的屬性(另一個對象)。例如。 'Properties p = new Properties(); p.putAll(p1);' –
謝謝你! – user3732793