Sometimes, We want to include special characters in the value of the properties file.

Let’s see some examples.

# include special characters

selectquery= select * from table where id=10

We can include below special characters to escape and read by a Java programmer

  • # symbol
  • = symbol
  • : symbol

escape # sign in the properties file

# character can be escaped with \ symbol in the properties file

# include special characters

name= first \\# last

How to escape equal sign in a properties file

Here is an example

# include special characters

selectquery= select * from table where id`\=`10

The same can be achieved with Java Unicode characters equal sign can be replaced with java Unicode \u003d value and white space for \u0020

# include special characters

selectquery=\u0020select\u0020*\u0020from\u0020table\u0020where\u0020id\u003d10

escape @ character in properties file

Sometimes, We want to escape the @ character in a properties file. For example, email value

support.email=support\\@domain.com

an encoded character we can use %40 in place of @

support.email=support%40domain.com

Escape backslash in properties files

For example, when storing the path containing backslashes, use double backslashes.

temp.location=\\tmp

Replace with backslash with \u005c using encoding character.

temp.location=\u005ctmp

When you use escaped characters, In a Java processor you have to read the processor content with encoding.

Conclusion

You learned many ways to use escape and special characters in properties.

You can use different ways or Unicode characters. Unicode characters are less readable.