'Attempts to write a colon character (":") to a Properties file result in the character being escaped [duplicate]
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?
Solution 1:[1]
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.
Solution 2:[2]
That's the normal behavior of the class. Read the javadocs for that:Properties
It says:
Characters that cannot be directly represented in this encoding can be written using Unicode escapes
The colon is one of these characters.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Lior Ohana |
