无法将URL保存为PropertiesConfiguration对象中的值

时间:2011-06-02 11:05:49

标签: java

这个让我难过。

我有类似的东西:

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

我将这段代码放在一个基于Linux的jar中。当它在Windows机器上运行时,保存的文件就像:

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

这个文件会被一些python代码占用,而且对这个URL一点也不满意。

我进行了搜索和搜索,但是对于我的生活,我不明白为什么PC会逃避正斜线。

任何线索?

3 个答案:

答案 0 :(得分:0)

我遇到了一个类似的问题,我可以通过使用反斜杠来解决它。尝试使用'\'代替'/'用于Windows系统。

答案 1 :(得分:0)

你确定它不是冒号而不是正斜线吗?

我用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);
  }
}

输出到文件是:

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

这是Properties.store中记载的,

  

键和元素字符#,!,   =,和:用前面的反斜杠写成,以确保它们是   装好了。

我怀疑你的解决方案是使用直接写入文件,而不是通过属性,例如

  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();
  }

答案 2 :(得分:0)

这似乎在版本2.0中得到修复,现在正在开发中。将我的依赖项更新为commons-configuration-2.0-SNAPSHOT后,我不再使用斜杠字符进行转义(在值中使用时)。