Properties.store() - suppress timestamp comment

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.


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...)


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, "");
    }