java - 使用java设置属性文件

标签 java file-io

我有一个带有一些文本字段的 Swing Frame,它们显示属性文件中的当前值。一旦我在文本字段中修改这些属性,它应该被保存回属性文件。我拥有的属性是数据库连接参数。我的连接参数如下

driver--org.postgresql.Driver
url--jdbc:postgresql://localhost/bank
user--postgres
password--aaa

但是当它更新时,在 url 字段中,凡是有“:”的地方,都会添加一个“\” 就像URL2=jdbc\:postgresql\://localhost/bank。我怎样才能避免这种情况?我尝试在设置属性文件之前打印内容,然后就可以了。 我在设置属性之前打印了字符串,它是正确的;

org.postgresql.Driver  **jdbc:postgresql://localhost/bank**postgres**aaa

有人可以帮助我吗? 提前致谢

 public static void update(String driver,String url, String user,String password) throws SecurityException, IOException{
        System.out.println(driver+"  **"+url+"**"+user+"**"+password);

        FileInputStream in = new FileInputStream("evaluator.properties");
        Properties props = new Properties();
        props.load(in);
        in.close();

        FileOutputStream out = new FileOutputStream("evaluator.properties");
        props.setProperty("Driver2", driver);
        props.setProperty("URL2", url);
        props.setProperty("Login2", user);
        props.setProperty("Password2", password);
        props.store(out, null);
        out.close();
}

最佳答案

Properties.store()转义某些字符:

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

一旦你用 Properties.load() 读回它们,它们不会被转义,因此您将获得原始值。

关于java - 使用java设置属性文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17080697/

相关文章:

java - 添加到数组列表功能上唯一的随机对象

java - 名字反写

Java 8 : How to get particular value within object inside list without converting stream back to list and fetch value at 0th location?

python - 找不到文件错误 : [Errno 2]

c - C语言中stderr的使用

c++ - ReadFileEx,可变长度——几个问题

android - 在 Android 中获取选定的图像文件位置

java - 从 jni 访问加速度计

java - 如何使用 JAXB 将复杂的 XML 元素映射到 Java 类属性

c - C 程序如何确定并打印它自己的可执行文件的位置?