簡體   English   中英

如何在屬性文件中轉義冒號 (:)?

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

我正在使用屬性文件來存儲我的應用程序的配置值。 在其中一種情況下,我必須將值存儲為xxx:yyy:zzz 當我這樣做時,冒號用反斜杠\\轉義,導致屬性文件中的值顯示為xxx\\:yyy\\:zzz

我知道冒號:Properties Java 類的標准分隔符。 但是我仍然需要保存沒有反斜杠\\的值。

有關如何處理此問題的任何建議?

將屬性放入Properties對象並使用store(...)方法保存它。 該方法將執行任何所需的轉義。 Java 文檔說:

"... 對於鍵,所有空格字符都以 \\ 字符開頭。對於元素,前導空格字符,但不包括嵌入或尾隨空格字符,以 \\ 字符開頭。鍵和元素字符 #, !、= 和 : 用前面的反斜杠寫入,以確保它們被正確加載。”

如果您手動創建/寫入文件,則只需要手動轉義字符。


相反,如果您希望文件包含未轉義的冒號字符,那您就不走運了。 這樣的文件格式錯誤,可能無法使用Properties.load(...)方法正確加載。 如果您沿着這條路線走下去,您將需要實現自己的自定義加載和/或存儲方法。

我遇到了同樣的問題。 正斜杠/也被Propertiesstore()方法轉義。

我創造我自己的解決了這個問題CustomProperties類(延伸java.util.Properties )和注釋掉調用saveConvert()customStore0()方法。

這是我的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();
  }
}

幾天前我們遇到了這個問題。 我們使用 URL 作為值來操作現有的屬性文件。

這是有風險的,但如果您的屬性值少於 40 個字符,那么您可以使用“list”方法而不是“store”:

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

我們快速瀏覽了 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 ),這應該不是問題。

它很簡單,只需在那里使用撇號' '例如:

而不是這個(案例1)

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

使用這個(案例2)

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

輸出將是


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

情況 2: basedir = f:\\\\properties\\\\gog\\\\esave\\\\apple

我希望這能幫到您

嘗試使用 unicode。

冒號的 unicode 是\:

此外,空格的 unicode 是: \

有關基本拉丁字符的列表,請參閱: https : //en.wikipedia.org/wiki/Basic_Latin_(Unicode_block)

例如:

ProperName\:\\NameContinues=Some property value

將期望帶有鍵的屬性:

ProperName:NameContinues

並將具有以下值:

Some property value

對我來說,它通過在特殊字符之前使用\\來工作,

例如,

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

:\\: (空格)與\\\\后跟 <空格>)。

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

對於像我這樣在使用 Spring Boot 配置屬性文件時到達這里的人:您需要包含在[..]

例如:

my.test\:key=value

還不夠,您需要在application.properties ,例如:

my.[test\:key]=value

另請參閱SpringBoot2 ConfigurationProperties 從 yaml 鍵中刪除冒號

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM