开发者

Properties.store() - suppress timestamp comment

Is it possible to force Properties not to add the date comment in front? I mean something like the first line here:

#Thu May 26 09:43:52 CEST 2011
main=pkg.ClientMain
args=myargs

I would like to get rid of it altogether. I need my config files 开发者_运维百科to be diff-identical unless there is a meaningful change.


Guess not. This timestamp is printed in private method on Properties and there is no property to control that behaviour.

Only idea that comes to my mind: subclass Properties, overwrite store and copy/paste the content of the store0 method so that the date comment will not be printed.

Or - provide a custom BufferedWriter that prints all but the first line (which will fail if you add real comments, because custom comments are printed before the timestamp...)


Given the source code or Properties, no, it's not possible. BTW, since Properties is in fact a hash table and since its keys are thus not sorted, you can't rely on the properties to be always in the same order anyway.

I would use a custom algorithm to store the properties if I had this requirement. Use the source code of Properties as a starter.


Based on https://stackoverflow.com/a/6184414/242042 here is the implementation I have written that strips out the first line and sorts the keys.

public class CleanProperties extends Properties {
    private static class StripFirstLineStream extends FilterOutputStream {

        private boolean firstlineseen = false;

        public StripFirstLineStream(final OutputStream out) {
            super(out);
        }

        @Override
        public void write(final int b) throws IOException {
            if (firstlineseen) {
                super.write(b);
            } else if (b == '\n') {
                firstlineseen = true;
            }
        }

    }

    private static final long serialVersionUID = 7567765340218227372L;

    @Override
    public synchronized Enumeration<Object> keys() {
        return Collections.enumeration(new TreeSet<>(super.keySet()));
    }

    @Override
    public void store(final OutputStream out, final String comments) throws IOException {
        super.store(new StripFirstLineStream(out), null);
    }
}

Cleaning looks like this

    final Properties props = new CleanProperties();
    try (final Reader inStream = Files.newBufferedReader(file, Charset.forName("ISO-8859-1"))) {
        props.load(inStream);
    } catch (final MalformedInputException mie) {
        throw new IOException("Malformed on " + file, mie);
    }
    if (props.isEmpty()) {
        Files.delete(file);
        return;
    }

    try (final OutputStream os = Files.newOutputStream(file)) {
        props.store(os, "");
    }


if you try to modify in the give xxx.conf file it will be useful.

The write method used to skip the First line (#Thu May 26 09:43:52 CEST 2011) in the store method. The write method run till the end of the first line. after it will run normally.

public class CleanProperties extends Properties {
    private static class StripFirstLineStream extends FilterOutputStream {

        private boolean firstlineseen = false;

        public StripFirstLineStream(final OutputStream out) {
            super(out);
        }

        @Override
        public void write(final int b) throws IOException {
            if (firstlineseen) {
                super.write(b);
            } else if (b == '\n') {
                // Used to go to next line if did use this line
                // you will get the continues output from the give file
                super.write('\n');

                firstlineseen = true;
            }
        }

    }


    private static final long serialVersionUID = 7567765340218227372L;

    @Override
    public synchronized Enumeration<java.lang.Object> keys() {
        return Collections.enumeration(new TreeSet<>(super.keySet()));
    }

    @Override
    public void store(final OutputStream out, final String comments)
        throws IOException {
        super.store(new StripFirstLineStream(out), null);
    }
}


Can you not just flag up in your application somewhere when a meaningful configuration change takes place and only write the file if that is set?

You might want to look into Commons Configuration which has a bit more flexibility when it comes to writing and reading things like properties files. In particular, it has methods which attempt to write the exact same properties file (including spacing, comments etc) as the existing properties file.


You can handle this question by following this Stack Overflow post to retain order:

Write in a standard order: How can I write Java properties in a defined order?

Then write the properties to a string and remove the comments as needed. Finally write to a file.

ByteArrayOutputStream baos = new ByteArrayOutputStream();
properties.store(baos,null);

String propertiesData = baos.toString(StandardCharsets.UTF_8.name());

propertiesData = propertiesData.replaceAll("^#.*(\r|\n)+",""); // remove all comments

FileUtils.writeStringToFile(fileTarget,propertiesData,StandardCharsets.UTF_8);

// you may want to validate the file is readable by reloading and doing tests to validate the expected number of keys matches
InputStream is = new FileInputStream(fileTarget);
Properties testResult = new Properties();
testResult.load(is);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜