【问题标题】:How to escape the equals sign in properties files如何在属性文件中转义等号
【发布时间】:2011-01-25 07:19:40
【问题描述】:

如何转义 Java 属性文件中的等号 (=)?我想在我的文件中添加如下内容:

table.whereclause=where id=100

【问题讨论】:

    标签: java properties escaping


    【解决方案1】:

    在您的具体示例中,您不需要转义等号 - 如果它是键的一部分,您只需要转义它。属性文件格式会将第一个非转义等于之后的所有字符视为值的一部分。

    【讨论】:

    • 不知道为什么没有标记出来!最简单的正确答案!大多数时候人们都在寻找属性文件中的转义值!
    • 小修正:将第一个未转义的等号和尾随空格之后的所有字符视为值,即取出值中的前导空格。示例“myKey=my value”与“myKey = my value”相同。
    【解决方案2】:

    另外,请参考load(Reader reader) method from Property class on javadoc

    load(Reader reader) 方法文档中它说

    密钥包含所有字符 在从第一个开始的行中 非空白字符和最多, 但不包括第一个未转义的 '='':' 或空白字符 除了行终止符。所有的 这些关键终止字符可能 通过转义被包含在密钥中 它们带有前面的反斜杠 特点;例如,

    \:\=
    

    将是两个字符的键 ":=". 行终止符可以是 包括使用 \r\n 转义 序列。之后的任何空白 键被跳过;如果第一个非白人 键后的空格字符是'='':',则它会被忽略并且任何 后面的空格字符是 也跳过了。所有剩余字符 上线成为其中的一部分 关联元素字符串;如果有 没有剩余的字符, element 是空字符串""。一次 原始字符序列 构成键和元素的是 识别,逃逸处理是 如上所述进行。

    希望对您有所帮助。

    【讨论】:

      【解决方案3】:

      Java 中的默认转义字符是“\”。
      但是,Java 属性文件的格式为 key=value,它应该将第一个相等之后的所有内容都视为值。

      【讨论】:

      • 除了前导空格(哦,键和值之间的“=”不是必需的,可以是:或空格)
      【解决方案4】:

      避免此类问题的最佳方法是以编程方式构建属性然后存储它们。例如,使用这样的代码:

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

      这会将正确转义的版本输出到 System.out。

      在我的情况下,输出是:

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

      如您所见,这是一种生成 .properties 文件内容的简单方法,可以保证正确无误。您可以根据需要放置任意数量的属性。

      【讨论】:

        【解决方案5】:

        就我而言,两个前导 '\\' 对我来说很好。

        例如: 如果您的单词包含“#”字符(例如 aa#100,您可以使用两个 前导 '\\'

           key= aa\\#100

        【讨论】:

        • 我同意,你需要两个反斜杠(glassfish 4.1,build 13)
        【解决方案6】:

        你可以看这里Can the key in a Java property include a blank character?

        转义等于'=' \u003d

        table.whereclause=其中 id=100

        键:[table.whereclause] 值:[其中 id=100]

        table.whereclause\u003dwhere id=100

        键:[table.whereclause=where] 值:[id=100]

        table.where 子句\u003dwhere\u0020id\u003d100

        键:[table.whereclause=where id=100] 值:[]

        【讨论】:

          【解决方案7】:

          在 Spring 或 Spring boot application.properties 文件中,这里是转义特殊字符的方法;

          table.whereclause=where id'\='100

          【讨论】:

            【解决方案8】:

            此方法应该有助于以编程方式生成保证与.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);
                }
            }
            

            你可以这样称呼它:

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

            这种方法有点贵,但是无论如何,将属性写入文件通常是零星的操作。

            【讨论】:

              【解决方案9】:

              我已经能够在字符“:”中输入值

              db_user="postgresql"
              db_passwd="this,is,my,password"
              

              【讨论】:

              • 为什么要投反对票?这是一个真正的答案:)
              • Op 表示 Java 属性,引号在 Java 属性文件中的工作方式不同。
              猜你喜欢
              • 2012-05-28
              • 1970-01-01
              • 1970-01-01
              • 2011-12-06
              • 1970-01-01
              • 2011-04-27
              • 2011-04-14
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多