简体   繁体   English

如何在属性文件中转义冒号 (:)?

[英]How do you escape colon (:) in Properties file?

I am using a properties file to store my application's configuration values.我正在使用属性文件来存储我的应用程序的配置值。 In one of the instances, I have to store a value as xxx:yyy:zzz .在其中一种情况下,我必须将值存储为xxx:yyy:zzz When I do that, the colon is escaped with a back slash \\ resulting in the value showing as xxx\\:yyy\\:zzz in the properties file.当我这样做时,冒号用反斜杠\\转义,导致属性文件中的值显示为xxx\\:yyy\\:zzz

I am aware that the colon : is a standard delimiter of the Properties Java class.我知道冒号:Properties Java 类的标准分隔符。 However I still need to save the value without the back slash \\ .但是我仍然需要保存没有反斜杠\\的值。

Any suggestions on how to handle this?有关如何处理此问题的任何建议?

Put the properties into the Properties object and save it using a store(...) method.将属性放入Properties对象并使用store(...)方法保存它。 The method will perform any escaping required.该方法将执行任何所需的转义。 The Java documentation says: Java 文档说:

"... For the key, all space characters are written with a preceding \\ character. For the element, leading space characters, but not embedded or trailing space characters, are written with a preceding \\ character. The key and element characters #, !, =, and : are written with a preceding backslash to ensure that they are properly loaded." "... 对于键,所有空格字符都以 \\ 字符开头。对于元素,前导空格字符,但不包括嵌入或尾随空格字符,以 \\ 字符开头。键和元素字符 #, !、= 和 : 用前面的反斜杠写入,以确保它们被正确加载。”

You only need to manually escape characters if you are creating / writing the file by hand.如果您手动创建/写入文件,则只需要手动转义字符。


Conversely, if you want the file to contain unescaped colon characters, you are out of luck.相反,如果您希望文件包含未转义的冒号字符,那您就不走运了。 Such a file is malformed and probably won't load properly using the Properties.load(...) methods.这样的文件格式错误,可能无法使用Properties.load(...)方法正确加载。 If you go down this route, you'll need to implement your own custom load and/or store methods.如果您沿着这条路线走下去,您将需要实现自己的自定义加载和/或存储方法。

I came across the same issue.我遇到了同样的问题。 Forward slashes / also get escaped by the store() method in Properties .正斜杠/也被Propertiesstore()方法转义。

I solved this issue by creating my own CustomProperties class (extending java.util.Properties ) and commenting out the call to saveConvert() in the customStore0() method.我创造我自己的解决了这个问题CustomProperties类(延伸java.util.Properties )和注释掉调用saveConvert()customStore0()方法。

Here is my CustomProperties class:这是我的CustomProperties类:

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;

public class CustomProperties extends Properties {
  private static final long serialVersionUID = 1L;
  @Override
  public void store(OutputStream out, String comments) throws IOException {
      customStore0(new BufferedWriter(new OutputStreamWriter(out, "8859_1")),
                   comments, true);
  }
  //Override to stop '/' or ':' chars from being replaced by not called 
  //saveConvert(key, true, escUnicode)
  private void customStore0(BufferedWriter bw, String comments, boolean escUnicode)
          throws IOException {
      bw.write("#" + new Date().toString());
      bw.newLine();
      synchronized (this) {
          for (Enumeration e = keys(); e.hasMoreElements();) {
              String key = (String) e.nextElement();
              String val = (String) get(key);
              // Commented out to stop '/' or ':' chars being replaced
              //key = saveConvert(key, true, escUnicode);
              //val = saveConvert(val, false, escUnicode);
              bw.write(key + "=" + val);
              bw.newLine();
          }
      }
      bw.flush();
  }
}

We hit this question a couple of days ago.几天前我们遇到了这个问题。 We were manipulating existing properties files with URLs as values.我们使用 URL 作为值来操作现有的属性文件。

It's risky but if your property values are less than 40 characters then you can use the "list" method instead of "store":这是有风险的,但如果您的属性值少于 40 个字符,那么您可以使用“list”方法而不是“store”:

http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html#list(java.io.PrintWriter) http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html#list(java.io.PrintWriter)

We had a quick look at the JDK code and hacked out a custom implementation of store that works for our purposes:我们快速浏览了 JDK 代码并修改了一个适用于我们目的的 store 的自定义实现:

public void store(Properties props, String propertyFilePath) throws FileNotFoundException {
    PrintWriter pw = new PrintWriter(propertyFilePath); 
    for (Enumeration e = props.propertyNames(); e.hasMoreElements();) {
        String key = (String) e.nextElement();
        pw.println(key + "=" + props.getProperty(key));
    }
    pw.close();
}

如果您使用属性文件的 xml 变体(使用loadFromXMLstoreToXML ),这应该不是问题。

Its simple, just use Apostrophe ' ' over there Eg:它很简单,只需在那里使用撇号' '例如:

Instead of this(case 1)而不是这个(案例1)

File file= new File("f:\\properties\\gog\\esave\\apple");
prop.setProperty("basedir",file.toString());

Use this(case 2)使用这个(案例2)

File file= new File("f':'\\properties\\gog\\esave\\apple");
prop.setProperty("basedir",file.toString());

Output will be输出将是


Case 1: basedir = f\\:\\\\properties\\\\gog\\\\esave\\\\apple案例 1: basedir = f\\:\\\\properties\\\\gog\\\\esave\\\\apple

Case 2: basedir = f:\\\\properties\\\\gog\\\\esave\\\\apple情况 2: basedir = f:\\\\properties\\\\gog\\\\esave\\\\apple

I hope this will help you我希望这能帮到您

Try using unicode.尝试使用 unicode。

The unicode for a colon is \:冒号的 unicode 是\:

Additionally the unicode for a space is: \ 此外,空格的 unicode 是: \

For a list of basic Latin characters see: https://en.wikipedia.org/wiki/Basic_Latin_(Unicode_block)有关基本拉丁字符的列表,请参阅: https : //en.wikipedia.org/wiki/Basic_Latin_(Unicode_block)

For example:例如:

ProperName\:\\NameContinues=Some property value

Will expect a property with a key:将期望带有键的属性:

ProperName:NameContinues

And will have a value of:并将具有以下值:

Some property value

For me it worked by using \\ before special character,对我来说,它通过在特殊字符之前使用\\来工作,

eg,例如,

Before: VCS\u003aIC\u0020Server\u003a=Migration
After: VCS\:IC\ Server\:=Migration

: is escaped with \\: and :\\: (space) with \\ ( \\ followed by <Space>). (空格)与\\\\后跟 <空格>)。

For more info : https://en.wikipedia.org/wiki/.properties欲了解更多信息: https : //en.wikipedia.org/wiki/.properties

For people like me that get here for this when using Spring Boot configuration properties files: You need to enclose in [..] :对于像我这样在使用 Spring Boot 配置属性文件时到达这里的人:您需要包含在[..]

Eg:例如:

my.test\:key=value

is not enough, you need this in your application.properties for example:还不够,您需要在application.properties ,例如:

my.[test\:key]=value

See also SpringBoot2 ConfigurationProperties removes colon from yaml keys另请参阅SpringBoot2 ConfigurationProperties 从 yaml 键中删除冒号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM