Reputation: 1
I'm trying to read a Configuration file using Apache Commons, modify some properties and then save the config file.
This is the code snippet
Configurations propConfig= new Configurations();
try {
FileBasedConfigurationBuilder<PropertiesConfiguration> builder=
propConfig.propertiesBuilder(pathToPropFile);
propConfig.setProperty("dtv.primary.db.url","jdbc\\:oracle\\:thin\\:@localhost\\:1521\\:db");
builder.save();
return 0;
} catch (ConfigurationException e) {
e.printStackTrace();
return -1;
}
pom.xml snippet
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-configuration2</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
</dependency>
The problem is that the string "jdbc\\:oracle\\:thin\\:@localhost\\:1521\\:db"
is not properly escaped on config file.
All the additional backslashes are present in config file.
I've read all the documentation, it seems the one above, is the correct way for escaping special characters.
If i use the Java built in Properties Class, the string "jdbc:oracle:thin:@localhost:1521:db"
is automatically escaped once store (jdbc\:oracle\:thin\:@localhost\:1521\:db
), but i have to use Apache Configurations otherwise i lose all the comments in the config file.
Any suggestions?
Upvotes: 0
Views: 722
Reputation: 4067
In .properties file you have to escape both \
and :
. Thus what you should have in the .properties file is jdbc\\\:oracle\\\:thin\\\:@localhost\\\:1521\\\:db
).
In order to achieve that you can convert PropertiesConfiguration to java.util.Properties and save those, and only use commons configuration when reading.
You can use ConfigurationConverter to do that.
Upvotes: 2