Reputation: 10153
I am using java.util.Properties#store()
method to save my properties to a file:
os = new BufferedOutputStream(new FileOutputStream(propertiesFile));
properties.store(os, null);
Default implementation of this method always writes comment as the first line with current timestamp
:
#Thu May 16 12:55:36 EDT 2013
This behavior is not desired in my application as I need to track all changes in my properties file. Is it possible to filter this comment line (and all others) from property file somehow?
I am particularly interested in "on the fly" solution without post processing of the file afterwards.
Upvotes: 2
Views: 427
Reputation: 115328
Yes, you can :).
First, you can extend Properties
and override store()
method. But this way is too complicated.
You can do better. Examine source code of store()
. It calls private method store()
that actually does the work. This method calls
bw.write("#" + new Date().toString());
in the beginning. You do not want to see this line. This means that you have to implement your own BufferedWriter
that ignores the first printed line.
public class IgnoreFirstLineBufferedWriter extends BufferedWriter {
private int lineCouner = 0;
// constructors - implement them yourself
@Override
public void write(String str) throws IOException {
if (lineCounter > 0) {
super.write(str);
}
lineCounter++;
}
}
Now you can use this customized BufferedWriter
when you are wrapping your FileOutputStream
and calling store()
:
props.store(new IgnoreFirstLineBufferedWriter(new OutputStreamWriter(new FileOutputStream("myprops.properties"))));
Upvotes: 2
Reputation: 2006
Date is not printed when you use storeToXML method. But properties will be stored in XML( Otherwise the best way to do it is to extend Properties class as explained before
Upvotes: 0
Reputation: 68715
This timestamp is printed in private method on Properties and there is no property to control that behaviour. If you want to change this behavior then you may need to subclass Properties, overwrite store method and copy/paste the content of the store0 method so that the date comment will not be printed.
Upvotes: 0