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
This question is related to
java
properties
escaping
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
_x000D_
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.
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.
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:[]
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.
I've been able to input values within the character ":
db_user="postgresql"
db_passwd="this,is,my,password"
In Spring or Spring boot application.properties file here is the way to escape the special characters;
table.whereclause=where id'\='100
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.
Source: Stackoverflow.com