我有一个带有一些文本字段的 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/