【发布时间】:2018-04-17 02:59:52
【问题描述】:
我正在将文件的路径作为使用 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中你可以只使用'/'而不是'\\'。这也确保了该路径可以在 linux 和 windows 上运行。
-
非常感谢!解决了:)
-
我如何从 File 对象中获取路径?
adb.getAbsolutePath().replace("\"."/")?还是有一个标准功能? -
this.adb.replace("\\", "/").replace("\\:", ":")不起作用。我得到adb=C\:/Program Files/Genymobile/Genymotion/tools/adb.exe。这与文件输出流有关吗? -
@MidhunVarghese 不,它与
java.util.Properties有关。您是否考虑过查阅 Javadoc?
标签: java javafx fileinputstream fileoutputstream