Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading multiple properties files

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?

like image 743
travega Avatar asked Apr 16 '12 20:04

travega


3 Answers

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

like image 100
Eugene Retunsky Avatar answered Oct 16 '22 11:10

Eugene Retunsky


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.

like image 35
tskuzzy Avatar answered Oct 16 '22 12:10

tskuzzy


Actually, yes. You can do this. If any of the properties overlap, the newer loaded property will take place of the older one.

like image 38
Petr Janeček Avatar answered Oct 16 '22 12:10

Petr Janeček