In my application, I attempt to write a URL string out to a property file. Of course, since it is a URL, it contains a colon (:
).
I noticed that when I call obj.setProperty("key","value")
, it escapes the colon with a backslash, passing \:
instead.
Here is some code that reproduces the behavior:
String url="http://google.co.in";
Properties p=new Properties();
FileOutputStream o=new FileOutputStream("abc.properties");
p.setProperties("testurl",url);
p.store(o,null);
o.close();
Why is this happening? Does the Properties class automatically escape certain characters when attempting to write them? Which characters are they?
Refer the Properties class's store method API. It says the characters #, !, =, and :
are saved with escaping backslash.
The key and element characters #, !, =, and : are written with a preceding backslash to ensure that they are properly loaded.
If you read the saved file back with load method in Properties class, there is no problem. If not, you'll have to write your own custom code to escape these characters while loading.