[英]Setting properties file using java
我有一个带有一些文本字段的Swing框架,它在Properties文件中显示当前值。 一旦在文本字段中修改了这些属性,就应将其保存回属性文件。 我拥有的属性是数据库连接参数。 我的连接参数如下
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()转义某些字符:
键和元素字符#,!,=和:前面带有反斜杠,以确保正确加载它们。
一旦使用Properties.load()
读回它们,它们将不转义,以便您获得原始值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.