owner icon indicating copy to clipboard operation
owner copied to clipboard

How to edit the .properties file by owner ?

Open Shynoo opened this issue 8 years ago • 2 comments

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 ?

Shynoo avatar Jan 04 '17 09:01 Shynoo

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.

drapostolos avatar Jan 04 '17 16:01 drapostolos

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.

Liso-cz avatar Nov 15 '18 19:11 Liso-cz