Is it possible to stack loaded properties in Java? For instance can I do:
Properties properties = new Properties();
properties.load(new FileInputStream("file1.properties"));
properties.load(new FileInputStream("file2.properties"));
and access properties from both?
You can do this:
Properties properties = new Properties(); properties.load(new FileInputStream("file1.properties")); Properties properties2 = new Properties(); properties2.load(new FileInputStream("file2.properties")); properties.putAll(properties2);
NOTE : All the keys maintained are unique. So, the later properties loaded with same key will be overridden. Just to keep for your ref :)
Yes the properties stack. Properties
extends Hashtable
and load()
simply calls put()
on each key-value pair.
Relevant code from the Source:
String key = loadConvert(lr.lineBuf, 0, keyLen, convtBuf);
String value = loadConvert(lr.lineBuf, valueStart, limit - valueStart, convtBuf);
put(key, value);
In other words, loading from a file doesn't clear the current entries. However, note that if the two files contain entries with the same key, the first one will be overwritten.
Actually, yes. You can do this. If any of the properties overlap, the newer loaded property will take place of the older one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With