java 在属性文件中读取 Windows 路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28890426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 14:21:27  来源:igfitidea点击:

reading windows paths in properties file

javaduplicates

提问by Carlos Alegría

I am reading windows file paths from a properties file in Java. I do really need my program to handle unscaped paths, but so far I cannot see how to do it.

我正在从 Java 中的属性文件中读取 Windows 文件路径。我确实需要我的程序来处理未转义的路径,但到目前为止我不知道该怎么做。

The properties key-value:

属性键值:

file.path = C:\directory

file.path = C:\目录

The code:

代码:

Properties properties = new Properties();
properties.load("filename.properties");
properties.getProperty("file.path");

The undesired result:

不希望的结果:

C:directory


UPDATE: Duplicate question stackoverflow.com/questions/5784895/java-properties-backslash

更新:重复问题stackoverflow.com/questions/5784895/java-properties-backslash

回答by

You can escape it using \\:

您可以使用\\

file.path = C:\directory

Or, you may also use forward slash instead:

或者,您也可以使用正斜杠代替:

file.path = C:/directory