Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape the equals sign in properties files

How can I escape the equals sign (=) in Java property files? I would like to put something as the following in my file:

table.whereclause=where id=100 
like image 855
joshua Avatar asked Mar 09 '10 06:03

joshua


People also ask

How do I escape the special characters in properties file?

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

How do you escape the equal sign?

If you have an equals sign in your value, you'll need to escape it with a backslash unless you've quoted the entire string.

How do I comment in application properties file?

In addition to properties and blank lines, the application. properties field may contain comments. To comment a line, just put the hash character at the beginning of a line.

What is in .properties file?

properties is a file extension for files mainly used in Java-related technologies to store the configurable parameters of an application. They can also be used for storing strings for Internationalization and localization; these are known as Property Resource Bundles.


2 Answers

In your specific example you don't need to escape the equals - you only need to escape it if it's part of the key. The properties file format will treat all characters after the first unescaped equals as part of the value.

like image 154
wrschneider Avatar answered Sep 25 '22 02:09

wrschneider


Moreover, Please refer to load(Reader reader) method from Property class on javadoc

In load(Reader reader) method documentation it says

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 ":=". Line terminator characters can be included using \r and \n escape sequences. Any white space after the key is skipped; if the first non-white space character after the key is '=' or ':', then it is ignored and any white space characters after it are also skipped. All remaining characters on the line become part of the associated element string; if there are no remaining characters, the element is the empty string "". Once the raw character sequences constituting the key and element are identified, escape processing is performed as described above.

Hope that helps.

like image 40
Mohd Farid Avatar answered Sep 27 '22 02:09

Mohd Farid