Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Properties file. Problems with setProperty() method

I try to overwrite existing property value in Properties file using Properties#setProperty() method.

But I get an extra backslash...

For example I have following entry inside Properties file:

#url to server
url=http://192.22.222.222

and when I try to overwrite value http://192.22.222.222 by new value http://192.33.333.333 I get followong result: http\://192.33.333.333

i.e. first backlash is unnecessary. I

Where is problem?

like image 949
WelcomeTo Avatar asked Sep 24 '12 07:09

WelcomeTo


3 Answers

There's no problem. When you load the file again, you won't see a backslash in the property value. The escaping code is choosing to escape all colons (and probably all equals signs) regardless of whether it's strictly required. (When they're not part of the key, you don't have to escape them, but it probably makes the code simpler to do so.)

So long as you're always loading the code using one of the Properties.load methods, you should see no problems at all.

like image 185
Jon Skeet Avatar answered Nov 19 '22 12:11

Jon Skeet


From the doc:

The key contains all of the characters in the line starting with the first non-white space character and up to, but not including, the first unescaped '=', ':', or white space character other than a line terminator. All of these key termination characters may be included in the key by escaping them with a preceding backslash character; for example,

\:\=

would be the two-character key ":=".

like image 26
sp00m Avatar answered Nov 19 '22 11:11

sp00m


Java properties will escape some characters like colon when writing a property value. See Properties#store.

This is considered a feature because the Properties format allows colons as key/value separators (see the source of Properties#load method)

like image 1
mauhiz Avatar answered Nov 19 '22 13:11

mauhiz