开发者

PropertiesConfiguration - Using "/" in Property value

I am using PropertiesConfiguration for loading and storing prope开发者_如何学Pythonrty values.

If I use character '/' in Property value, it gets saved as'\/'. Could you please help me in saving just '/' instead of '\/'? Am I missing something here?

PropertiesConfiguration databaseProperties;
databaseProperties = new PropertiesConfiguration("Properties.properties");
databaseProperties.setProperty("PropName", "jdbc:sqlserver://");
databaseProperties.save();

After saving my property file has value as

PropName = jdbc:sqlserver:\/\/

Thanks,


I used 1.10 version of commons-configuration and works fine:

<dependency>
    <groupId>commons-configuration</groupId>
    <artifactId>commons-configuration</artifactId>          
    <version>1.10</version>
</dependency>


The escaping of / is done to fulfill the requirements of the properties file format. This format is described at the JavaDoc of Properties.load(Reader).

If that's not what you want, then you don't really want a properties file, but some other (possibly similar) format.


This is because "/" has to be escaped for it to read back into the system. You could open the file back up and unscape those values but it would cause problems for you the next time you tried to open it.


Are you sure it doesn't look like

PropName=jdbc\:sqlserver\://

and you typed this question in from memory? / is not noramally an escaped character in a properties file, but : is.

Perhaps the fact that you're not using a java.util.Properties file has a lot to do with it. The writers of PropertiesConfiguration probably modified the load / store slightly to create properties files which better handle the / character when read in as shell scripts.

Some systems read in properties files as "sourced shell scripts" to provide shell oriented command line tools which are compatible with the settings of the java program's properties files. If this is the case, then escaping the / would prevent it from being interperted as a directory separator in certain contexts in the shell script.


Regarding Joachim's answer, I have looked at the javadoc to which he linked, and I even followed the links posted therein to the Java Language reference, and nowhere do I see where a slash ('/') in a property value would need to be escaped. When I use a java.util.Properties, with its methods load() and save(), slashes in property values are not escaped. For that matter, neither are colons (':') if used after the first property key delimiter.

For my own use, I updated my maven dependency to use commons-configuration version 2.0-SNAPSHOT, and this seems to be fixed. Having looked at the code (PropertiesConfiguration and PropertiesConfigurationLayout), it would not surprise me if it was a bug or a quirk in the commons-lang dependency. Commons-configuration 1.9 uses commons-lang 2.6, but commons-configuration 2.0-SNAPSHOT uses commons-lang3.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜