我试图加载属性,如果它不存在创建一个。加载属性,如果不存在 - 创建它
好了,下面是装载:
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