2

I have a Swing Frame with some text-fields which displays the current values in the Properties file. Once I modify these properties in the text-field it should be saved back to the properties file. The properties which I have are database connection parameters. My connection parameters are as follows

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

But when it updates, in the url field, where ever there is a ':', it adds a '\' like URL2=jdbc\:postgresql\://localhost/bank. How can I avoid this? I tried printing the contents before setting the Properties file and then it is ok. I printed the String before setting the properties, there it is coming right;

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

Can someone please help me. Thanks in Advance

 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();
}
4
  • 1
    It doesn't matter, it will work all the same; but yeah, it is annoying Commented Jun 13, 2013 at 6:42
  • This is how the properties API works, it needs escape certain characters. When you read the properties back in (via the Properties class) how do they look? Commented Jun 13, 2013 at 6:42
  • As @MadProgrammer mentioned, if you add a props.list(System.err); directly after your props.load(in); line, you can verify that after reloading the properties look fine Commented Jun 13, 2013 at 6:46
  • @MadProgrammer:thanks. When i read the URL back, it displays in the way I need. Commented Jun 13, 2013 at 6:50

2 Answers 2

3

Properties.store() escapes certain characters:

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

Once you read them back with Properties.load(), they are unescaped so that you will get the original value.

Sign up to request clarification or add additional context in comments.

3 Comments

your typing speed is fast than me ... :-p
@Shinchan It primarily depends on how fast you find the docs ;-)
@Andrew:Thanks a lot for the explanantion
1

As per java docs for store API , some characters will be escaped while writing to an outputstream.

So, the colon ":" is getting converted to "\:"

I hope the solution would be to escape the characters before writing to outputstream. While converting back to properties instance, the escape characters will be treated as required.

1 Comment

Thanks for the explanantion

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.