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

旧巷老猫 提交于 2019-12-28 03:05:15

问题


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. 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.

I am aware that the colon : is a standard delimiter of the Properties Java class. However I still need to save the value without the back slash \.

Any suggestions on how to handle this?


回答1:


Put the properties into the Properties object and save it using a store(...) method. The method will perform any escaping required. The Java documentation says:

"... 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. If you go down this route, you'll need to implement your own custom load and/or store methods.




回答2:


I came across the same issue. Forward slashes / also get escaped by the store() method in Properties.

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.

Here is my CustomProperties class:

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();
  }
}



回答3:


We hit this question a couple of days ago. We were manipulating existing properties files with URLs as values.

It's risky but if your property values are less than 40 characters then you can use the "list" method instead of "store":

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:

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();
}



回答4:


If you use the xml variant of the properties file (using loadFromXML and storeToXML) this shouldn't be a problem.




回答5:


Its simple, just use Apostrophe ' ' over there E.g.:

Instead of this(case 1)

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

Use this(case 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

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

I hope this will help you




回答6:


Try using unicode.

The unicode for a colon is\u003A

Additionally the unicode for a space is: \u0020

For a list of basic Latin characters see: https://en.wikipedia.org/wiki/Basic_Latin_(Unicode_block)

For example:

ProperName\u003A\NameContinues=Some property value

Will expect a property with a key:

ProperName:NameContinues

And will have a value of:

Some property value



来源:https://stackoverflow.com/questions/10699055/how-do-you-escape-colon-in-properties-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!