Java 如何转义属性文件中的等号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2406975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to escape the equals sign in properties files
提问by joshua
How can I escape the equals sign (=
) in Java property files? I would like to put something as the following in my file:
如何转义=
Java 属性文件中的等号 ( )?我想在我的文件中放入以下内容:
table.whereclause=where id=100
采纳答案by Mohd Farid
Moreover, Please refer to load(Reader reader)method from Property
class on javadoc
此外,请参考javadoc 类中的load(Reader reader)方法Property
In load(Reader reader)
method documentation it says
在load(Reader reader)
方法文档中它说
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.
关键包含了所有的从第一个非空格字符以及高达行的字符,但不包括第一转义
'='
,':'
或比行结束符等空白字符。所有这些键终止字符都可以通过使用前面的反斜杠字符将它们转义来包含在键中;例如,\:\=
将是两个字符的键
":=".
可以使用\r
和\n
转义序列包含行终止符。跳过键后的任何空格;如果键后的第一个非空白字符是'='
or':'
,则忽略它并跳过它后面的任何空白字符。该行所有剩余的字符成为相关元素字符串的一部分;如果没有剩余字符,则元素为空字符串""
。一旦识别出构成键和元素的原始字符序列,就如上所述执行转义处理。
Hope that helps.
希望有帮助。
回答by Padmarag
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.
Java 中的默认转义字符是“\”。
但是,Java 属性文件的格式为 key=value,它应该考虑第一个等于 value 之后的所有内容。
回答by Veaceslav Serghienco
You can look here Can the key in a Java property include a blank character?
您可以在这里查看Java 属性中的键可以包含空白字符吗?
for escape equal '=' \u003d
转义等于 '=' \u003d
table.whereclause=where id=100
table.whereclause=where id=100
key:[table.whereclause] value:[where id=100]
键:[table.whereclause] 值:[其中 id=100]
table.whereclause\u003dwhere id=100
table.whereclause\u003dwhere id=100
key:[table.whereclause=where] value:[id=100]
键:[table.whereclause=where] 值:[id=100]
table.whereclause\u003dwhere\u0020id\u003d100
table.whereclause\u003dwhere\u0020id\u003d100
key:[table.whereclause=where id=100] value:[]
键:[table.whereclause=where id=100] 值:[]
回答by wrschneider
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.
在您的具体示例中,您不需要转义等号 - 如果它是密钥的一部分,您只需要转义它。属性文件格式会将第一个未转义的等于之后的所有字符视为值的一部分。
回答by mvmn
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.
这将输出到 System.out 正确转义的版本。
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.
如您所见,这是生成 .properties 文件内容且保证正确的一种简单方法。您可以根据需要放置任意数量的属性。
回答by Monsif EL AISSOUSSI
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 '\\'
例如:如果您的单词包含 '#' 字符(例如 aa#100,您可以使用两个前导 '\\' 对其进行转义
key= aa\#100
回答by russellhoff
I've been able to input values within the character ":
我已经能够在字符“中输入值:
db_user="postgresql"
db_passwd="this,is,my,password"
回答by sent.ror
In Spring or Spring boot application.properties file here is the way to escape the special characters;
在 Spring 或 Spring 启动 application.properties 文件中,这里是转义特殊字符的方法;
table.whereclause=where id'\='100
table.whereclause=where id'\\='100
回答by DanielCuadra
This method should help to programmatically generate values guaranteed to be 100% compatible with .properties
files:
此方法应该有助于以编程方式生成保证与.properties
文件100% 兼容的值:
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.
这种方法有点贵,但是,无论如何,将属性写入文件通常是一个零星的操作。