2011-06-02 50 views
2

这一个让我难住。无法将URL保存为PropertiesConfiguration对象中的值

我有类似:

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

我在这是在Linux上构建一个罐子此代码。当它被Windows设备上运行,那么被保存的文件出来,如:

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

这个文件是会得到一些Python代码消耗,它不是在所有的快乐与URL。

我已经搜索和搜索,但对于我的生活,我不明白为什么PC会逃脱一个正斜杠。

任何线索?

+0

任何解决方案? – danieln 2014-04-30 08:14:26

回答

0

我遇到了类似的问题,我可以通过使用反斜杠来解决它。尝试使用'\'而不是'/'作为Windows系统。

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

这似乎是在版本2.0中修复的,现在正在开发中。更新我的依赖关系到commons-configuration-2.0-SNAPSHOT后,我不再使用斜杠字符(在值中使用时)。