Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape special characters in the key of properties file?

I've got a key = value property in the .properties file:

give names: (1) code = xxx

... but when I tried to get that key, it threw an error:

No message found under code give names: (1) code = xxx

I tried escaping the whitespace with \ but it didn't work.

Do I need to escape :,(, and ) characters as well?

like image 847
user1169474 Avatar asked Jun 14 '13 14:06

user1169474


People also ask

How do you escape special characters in Java properties file?

In the name or value, Java language escape sequences \t , \n , \r , \f , \\ , \" , \' , and \uxxxx are recognized and converted to single characters. Any other escape sequence will be converted to the character after the backlash (e.g. \x will be converted to x ).

How do you escape a hash character in properties file?

Since it is java, you need to replace hash( # ) with \\# in your code. Notice the double slashes. Alternately you can replace # with \\u0023 .

How do I get the key of a properties file?

Get All Key Values from Properties File Get All Key Values from Properties File in java using the Properties class Available in java. util Package. The Properties class extends the Hashtable class. From Hashtable, Properties class inherits the Method KeySet() which Returns a Set view of the keys Contained in this Map.


2 Answers

In my case, two leading '\\' working fine for me.

For example : if your word contains the '$' character (e.g. Rf$RF, you can escape it with two leading '\\'

like image 167
Devendrakumar Gaikwad Avatar answered Oct 15 '22 12:10

Devendrakumar Gaikwad


You could check out: http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html#load(java.io.Reader)

For info on how java interprets a properties file. The most relevant part is:

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.

like image 34
Emil H Avatar answered Oct 15 '22 11:10

Emil H