Java属性,获取文件路径 [英] Java Properties, getting file path
问题描述
logpath = LoggerUtils.getProperties().getProperty("log.path");
System.out.println("logpath: " + logpath);
上面的代码返回:
logpath: C:UsersMauriceDesktopLogs
在属性文件中是:
log.path C:\Users\Maurice\Desktop\Logs
如何保留文件分隔符?我希望它也可以在Linux上运行,而不仅仅是Windows.
How do I retain the file separators? I want this to work on Linux as well and not just Windows.
推荐答案
实际上,您需要将其放在属性文件中:
Actually, you need to put this in the property file:
log.path C:\\Users\\Maurice\\Desktop\\Logs
看到这个:
更精确地load
方法:
向下滚动一点,您将看到以下内容:
Scroll down a bit and you will see this among other things:
该方法不会在无效的转义字符之前将反斜杠字符\视为错误;反斜杠默默地掉了下来.例如,在Java字符串中,序列"\ z"将导致编译时错误.相比之下,此方法会静默删除反斜杠.因此,此方法将两个字符序列"\ b"等同于单个字符"b".
The method does not treat a backslash character, \, before a non-valid escape character as an error; the backslash is silently dropped. For example, in a Java string the sequence "\z" would cause a compile time error. In contrast, this method silently drops the backslash. Therefore, this method treats the two character sequence "\b" as equivalent to the single character 'b'.
反斜杠\
是一个转义字符,否则将被静默删除.
Backslash \
is an escape character that is silently dropped otherwise.
这篇关于Java属性,获取文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!