I’m trying to load properties, and if it is not exist to create one.
ok, here is loading:
InputStream is = Store.class.getResourceAsStream("my.properties");
props.load(is);
but the hard part is to determinate if file not exist and create it if necessary
tried this one:
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();
}
what I can do in this case?
Actually if the file does not exist, the
getResourcemethod will returnnull, so you will get aNullPointerExceptionboth inside try and inside catch blocks.I don’t know actually whether it is anyway possible to create the desired file, because at first you need its complete path, which you cannot get because the method
getResourceorgetResourceAsStreamwill returnnull.What you can do is to get the path of another resource file that already exists inside the same folder and use it to get the path of the parent folder then add the desired file name to it.
But you should notice that project resources are not actually intended to be created at runtime. They should be added at development-time and usually only read (not modified).
If you need some files to update at runtime, you should use the normal file system.