【问题标题】:Java properties file specs [duplicate]Java 属性文件规范 [重复]
【发布时间】:2011-01-12 14:20:26
【问题描述】:

可能重复:
Escaping equal sign in properties files

在 .properties 文件中,我可以在第一个之后使用字符“=”吗?

像这样:

url=http://www.example.org/test=

是否允许?如果确实允许,我在哪里可以知道是否允许?

到目前为止,它似乎正在工作,但我只是不太确定它以后不会中断。

【问题讨论】:

标签: java properties specifications


【解决方案1】:

除非他们更改属性规范,否则它将始终有效。有关规格,请参阅 http://download.oracle.com/javase/6/docs/api/java/util/Properties.html#load%28java.io.Reader%29

【讨论】:

    【解决方案2】:

    您可以在=: 之前放置反斜杠转义字符(\)

    或者更好地使用下面的代码来打印出你的属性应该如何转义

                    Properties props = new Properties();
                    props.setProperty("url", "http://www.example.org/test=");
                    props.store(System.out, null);
    

    输出:

    #Wed Jan 12 14:30:39 GMT 2011
    url=http\://www.example.org/test\=
    

    另外,请查看Java API information

    【讨论】:

    • 这是不正确的。您无需转义 =: 字符,除非它们位于属性的 KEY 中,而不是 VALUE 中。
    • 是的,正如@DawoodibnKareem 所说,这是不正确的。仅当值以 : 开头时,您必须对其进行转义,否则 \ 将被忽略
    • 我写的是“你可以放”而不是“你应该放”。除此之外,我还粘贴了 Java6 Properties store() 方法的默认行为。
    猜你喜欢
    • 1970-01-01
    • 2019-04-03
    • 1970-01-01
    • 2011-12-26
    • 2014-04-08
    • 2017-05-15
    • 1970-01-01
    • 2019-06-14
    • 2012-04-26
    相关资源
    最近更新 更多