开发者

Cannot save a url as a value in a PropertiesConfiguration object

This one has me stumped.

I have something like:

PropertiesConfiguration pc = new PropertiesConfiguration();
pc.setProperty("url", "jdbc:postgres://localhost:5432/somedb");
pc.setBasePath(".");
pc.setFileName("my.properties");

I have this code in a jar that was built on Linux. When it gets run on a Windows box, then the file that gets saved comes out like:

url = jdbc:postgres:\/\/localhost:5432/somedb.

This file is going to get consumed by some python code, and it's not at all happy with that URL.

I've searched and searched, but for the life of me, I don't understand why PC would be escaping a forw开发者_StackOverfloward slash.

Any clues?


I faced a similar issue i was able to resolve it by using backward slash. Try to use '\' instead of '/' for windows system.


Are you sure it is not the colons rather than the forward slashes which get escaped?

I tried this with java.util.Properties:

public class PropertiesTest {  
  public static void main(String[] args) throws Exception {
    Properties props = new Properties();
    props.put("url", "jdbc:postgres://localhost:5432/somedb");
    OutputStream out = new FileOutputStream("my.properties");
    props.store(out, null);
  }
}

The output to file is:

url=jdbc\:postgres\://localhost\:5432/somedb

This is as documented in Properties.store, which says

The key and element characters #, !, =, and : are written with a preceding backslash to ensure that they are properly loaded.

I suspect your solution will be to use straight write to file, rather than going through Properties, e.g.

  public static void main(String[] args) throws Exception {
    File props = new File("my_raw.properties");
    BufferedWriter writer = new BufferedWriter(new FileWriter(props));
    writer.write("url=jdbc:postgres://localhost:5432/somedb");
    writer.close();
  }


This appears to be fixed in version 2.0, which is undergoing development now. Having updated my dependency to commons-configuration-2.0-SNAPSHOT, I no longer get slash characters escaped (when used in a value).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜