在 Properties JavaFX 中保存路径时删除转义字符
Removing escape characters when saving a Path in Properties JavaFX
我正在使用 Java 中的属性将文件路径保存到配置文件中。
this.adb = adb.getAbsolutePath();
this.prop.setProperty("adb", this.adb);
//save config to project root folder
this.prop.store(new FileOutputStream("config"), null);
保存后的配置文件内容为:
adb=C\:\Program Files\Genymobile\Genymotion\tools\adb.exe
如何保存它没有转义字符以便用户可以轻松地手动修改路径而无需输入转义字符。
我也尝试在不使用转义字符的情况下手动保存配置文件,但程序将 属性 读取为:
C:Program FilesGenymobileGenymotion oolsadb.exe
从配置文件中读取属性的代码是:
prop.load(new FileInputStream("config"));
this.adb = prop.getProperty("adb");
java.util.Properties.store uses backslash to escape special characters (e.g. equal sign in properties keys), so a literal backslash itself will need to be escaped \. If this behaviour is not what you want, don't use java.util.Properties, roll out your own utility Properties class it should be a simple exercise. If your not sure what an escape character is check this Wikipedia page.
Properties中的相关代码class(saveConvert方法)
if (aChar == '\') {
outBuffer.append('\'); outBuffer.append('\');
continue;
}
我正在使用 Java 中的属性将文件路径保存到配置文件中。
this.adb = adb.getAbsolutePath();
this.prop.setProperty("adb", this.adb);
//save config to project root folder
this.prop.store(new FileOutputStream("config"), null);
保存后的配置文件内容为:
adb=C\:\Program Files\Genymobile\Genymotion\tools\adb.exe
如何保存它没有转义字符以便用户可以轻松地手动修改路径而无需输入转义字符。
我也尝试在不使用转义字符的情况下手动保存配置文件,但程序将 属性 读取为:
C:Program FilesGenymobileGenymotion oolsadb.exe
从配置文件中读取属性的代码是:
prop.load(new FileInputStream("config"));
this.adb = prop.getProperty("adb");
java.util.Properties.store uses backslash to escape special characters (e.g. equal sign in properties keys), so a literal backslash itself will need to be escaped \. If this behaviour is not what you want, don't use java.util.Properties, roll out your own utility Properties class it should be a simple exercise. If your not sure what an escape character is check this Wikipedia page.
Properties中的相关代码class(saveConvert方法)
if (aChar == '\') {
outBuffer.append('\'); outBuffer.append('\');
continue;
}