90

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

9 Answers 9

118

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.

2
  • 13
    don't know why this has not been marked up ! Simplest correct answer ! And most of the times people are looking for escaping value in properties file !
    – Tejas
    Sep 6, 2013 at 10:57
  • 2
    Small correction: will treat as value all characters after the first unescaped equals and trailing spaces, that is, leading spaces in the value are taken out. Example "myKey=my value" is the same as "myKey = my value". Oct 19, 2017 at 11:44
92

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.

27

Default escape character in Java is '\'.
However, Java properties file has format key=value, it should be considering everything after the first equal as value.

3
  • 9
    except leading whitespace (oh and the '=' between key and value isn't required, it could be : or a blank space)
    – Brad Cupit
    Dec 12, 2011 at 19:17
  • is there any way to read '\' from properties file ? (my)Java application used to read the password from properties file which is contains special character's . Example : \s9JPT,IS59j=EMS , when i am reading this value from properties file i am getting only - s9JPT,IS59j=EMS - which is not correct .
    – Vasanth
    May 5, 2023 at 2:06
  • @Vasanth In that case, you have to escape the backslash ( \\s9JPT,IS59j=EMS ). Aug 18, 2023 at 18:26
21

The best way to avoid this kind of issues it to build properties programmatically and then store them. For example, using code like this:

java.util.Properties props = new java.util.Properties();
props.setProperty("table.whereclause", "where id=100");
props.store(System.out, null);

This would output to System.out the properly escaped version.

In my case the output was:

#Mon Aug 12 13:50:56 EEST 2013
table.whereclause=where id\=100

As you can see, this is an easy way to generate content of .properties files that's guaranteed to be correct. And you can put as many properties as you want.

0
8

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

For example : if your word contains the '#' character (e.g. aa#100, you can escape it with two leading '\\'

   key= aa\\#100

1
  • I second that, you need two backslashes (glassfish 4.1, build 13)
    – Susanta
    Dec 2, 2015 at 21:01
2

You can look here Can the key in a Java property include a blank character?

for escape equal '=' \u003d

table.whereclause=where id=100

key:[table.whereclause] value:[where id=100]

table.whereclause\u003dwhere id=100

key:[table.whereclause=where] value:[id=100]

table.whereclause\u003dwhere\u0020id\u003d100

key:[table.whereclause=where id=100] value:[]

2

In Spring or Spring boot application.properties file here is the way to escape the special characters;

table.whereclause=where id'\='100

1

This method should help to programmatically generate values guaranteed to be 100% compatible with .properties files:

public static String escapePropertyValue(final String value) {
    if (value == null) {
        return null;
    }

    try (final StringWriter writer = new StringWriter()) {
        final Properties properties = new Properties();
        properties.put("escaped", value);
        properties.store(writer, null);
        writer.flush();

        final String stringifiedProperties = writer.toString();
        final Pattern pattern = Pattern.compile("(.*?)escaped=(.*?)" + Pattern.quote(System.lineSeparator()) + "*");
        final Matcher matcher = pattern.matcher(stringifiedProperties);

        if (matcher.find() && matcher.groupCount() <= 2) {
            return matcher.group(matcher.groupCount());
        }

        // This should never happen unless the internal implementation of Properties::store changed
        throw new IllegalStateException("Could not escape property value");
    } catch (final IOException ex) {
        // This should never happen. IOException is only because the interface demands it
        throw new IllegalStateException("Could not escape property value", ex);
    }
}

You can call it like this:

final String escapedPath = escapePropertyValue("C:\\Users\\X");
writeToFile(escapedPath); // will pass "C\\:\\\\Users\\\\X"

This method a little bit expensive but, writing properties to a file is typically an sporadic operation anyway.

-2

I've been able to input values within the character ":

db_user="postgresql"
db_passwd="this,is,my,password"
1
  • 4
    Op says Java properties, quotes don't work like that in java properties files.
    – teknopaul
    May 14, 2018 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.