Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Value in properties file split by comma, reading file only loads first element

I have a properties file called configuration.properties, within configuration.properties is the key-value pair:

email.recipients = [email protected], [email protected]

In my Util.java class I load the configuration.properties file:

import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.ConfigurationException;

PropertiesConfiguration config = new PropertiesConfiguration("configuration.properties");
EMAIL_RECIPIENT_STRING = config.getString("email.recipients");

I expected to have EMAIL_RECIPIENT_STRING = "[email protected], [email protected]", but I get EMAIL_RECIPIENT_STRING = "[email protected]" only. What's the reason for this happening?

like image 225
Yinyin Wang Avatar asked Oct 19 '25 13:10

Yinyin Wang


2 Answers

It appears you're using Apache's PropertiesConfiguration. The docs states

value can contain value delimiters and will then be interpreted as a list of tokens. Default value delimiter is the comma ','.

getString only returns the first token. You need to use getStringArray to return all the properties

String recipients = config.getStringArray("email.recipients");
like image 150
Reimeus Avatar answered Oct 21 '25 04:10

Reimeus


Actually propConfig.setDelimiterParsingDisabled(true) is working, but you must load the config file after this setting, for example:

propConfig = new PropertiesConfiguration();
propConfig.setDelimiterParsingDisabled(true);
propConfig.load(propertiesFile);

if your code like is :

propConfig = new PropertiesConfiguration(propertiesFile); 
propConfig.setDelimiterParsingDisabled(true);

then the setting won't work

like image 29
kain Avatar answered Oct 21 '25 02:10

kain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!