owner
owner copied to clipboard
How to edit the .properties file by owner ?
I couldn't find the way to edit the .properties file . And the XxxConfig Class from Config interface is method rather than field.
So how could I edit the .properties file ?
Not sure this is what you are looking for, but the Mutable and Accessible interfaces provides a way to edit and store properties. See this.
There is store(file, append); method in interface Accessible. I find this a bit confusing, at first I was looking for this method in Mutable. Anyway, the weak support for persisting changed values is from my point of view a big disadvantage of Owner.
I used Owner for simple app configuration. Task slightly changed later and I wanted to store values changed by user. The problem was to access file paths in Sources annotation. After I got over it and extracted all classes needed from Owner. I expanded the first path beginning with "file:". And then I simply used store(...) method.
Here is my (awful, but working) code:
FileOutputStream output = null;
try {
Config.Sources sources = UserConfig.class.getAnnotation(Config.Sources.class);
UriFactory uriFactory = new UriFactory(this.getClass().getClassLoader());
URI uri = uriFactory.newURI(sources.value()[0]);
/* file: needs to be removed, otherwise its not working in Windows */
String filename = StringUtils.remove(uri.toURL().toExternalForm(), "file:");
final File file = new File(filename);
output = FileUtils.openOutputStream(file, false);
userConfig.store(output, "No comment");
} catch (URISyntaxException | IOException e) {
e.printStackTrace();
} finally {
if (output != null) {
try {
output.flush();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
I think that a little more convenient way to store mutable properties would be very handy. Otherwise this library is great and very practical.