我試圖加載屬性,如果它不存在創建一個。加載屬性,如果不存在 - 創建它
好了,下面是裝載:
InputStream is = Store.class.getResourceAsStream("my.properties");
props.load(is);
,但難的是確定的,如果不存在的文件,並創建它,如果必要
嘗試這一個:
File conf = null;
try {
conf = new File(Store.class.getResource("my.properties").getFile());
}
catch (Exception e){
// can't create file because getResource returns null again
conf = new File(Store.class.getResource("my.properties").getFile()); //WRONG
conf.createNewFile();
}
我可以在這種情況下做?
http://docs.oracle.com/javase/1.4。2/docs/api/java/io/File.html#exists%28%29 – hovanessyan 2012-02-17 12:55:55
@hovanessyan您不能使用exists方法,因爲conf將爲空 – VextoR 2012-02-17 12:57:31
您使用的構造函數:http://docs.oracle.com /javase/1.4.2/docs/api/java/io/File.html#File%28java.lang.String%29 – hovanessyan 2012-02-17 13:02:28