Java属性反斜杠
我正在使用Java属性读取属性文件。 一切正常,但是Properties静默删除反斜杠。
(即)
original: c:\sdjf\slkdfj.jpg
after: c:sdjfslkdfj.jpg
我如何使Properties不这样做?
我正在使用代码prop.getProperty(key)
我正在从文件获取属性,并且我想避免添加双反斜杠
JavaIsGreat asked 2020-06-17T00:39:09Z
8个解决方案
49 votes
正是由于Properties.load()引起了您看到的问题,因为反斜杠用于特殊用途。
保存所有数据的逻辑行 对于一个关键元素对可能会传播 跨越几个相邻的自然 转义行终止符 带有反斜杠字符的序列, \。
如果您无法使用CoolBeans的建议,那么您可以做的是先将属性文件读取为字符串,然后用双反斜杠替换反斜杠,然后将其提供给Properties.load()
String propertyFileContents = readPropertyFileContents();
Properties properties = new Properties();
properties.load(new StringReader(propertyFileContents.replace("\\", "\\\\")));
Bala R answered 2020-06-17T00:39:32Z
18 votes
使用双反斜线c:\\sdjf\\slkdfj.jpg
Properties props = new Properties();
props.setProperty("test", "C:\\dev\\sdk\\test.dat");
System.out.println(props.getProperty("test")); // prints C:\dev\sdk\test.dat
将信用更新到下面的@ewh。 显然,Windows可以识别斜杠。 因此,我想您可以让用户用反斜杠来代替它,如果以后需要反斜杠可以进行替换。 我在下面测试了此代码段,效果很好。
Properties props = new Properties();
props.setProperty("test", "C:/dev/sdk/test.dat");
System.out.println(props.getProperty("test")); // prints C:/dev/sdk/test.dat
CoolBeans answered 2020-06-17T00:39:57Z
10 votes
使用正斜杠。 Java从来不需要在文件名中使用反斜杠。
user207421 answered 2020-06-17T00:40:17Z
6 votes
如果确实需要在要加载的属性文件中使用反斜杠(例如,对于不是文件路径的属性),请为每个反斜杠字符输入DOMAIN\USERNAME。
反斜杠在@unhillbilly提供的文档中指出的属性文件中进行了特殊处理。
@EJP:例如,如果您要将NTLM登录ID存储在属性文件中(格式为DOMAIN\USERNAME,带有反斜杠),则绝对需要反斜杠。 此类型的属性不是文件名,因此正斜杠将不起作用。
编辑:@Max Nanasy:从上面提到的文档(java.util.Properties加载javadoc)中(强调我的)
该方法不会在无效的转义字符之前将反斜杠字符'\'视为错误; 反斜杠默默地掉了下来。 例如,在Java字符串中,序列“ \ z”将导致编译时错误。 相反,此方法会静默删除反斜杠。 因此,此方法将两个字符序列“ \ b”等同于单个字符“ b”
对我来说,除非我指定了unicode,否则我总是在属性文件中遇到反斜杠的问题(即使使用双反斜杠'\\')也是如此。
frooble answered 2020-06-17T00:41:00Z
2 votes
除了Bala R的答案外,我还有以下解决方案,甚至可以将反斜杠的换行语义保留在行尾。
这是我的代码:
private static Reader preparePropertyFile(File file) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(file));
StringBuilder result = new StringBuilder();
String line;
boolean endingBackslash = false;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (endingBackslash) {
// if the line is empty, is a comment or holds a new property
// definition the backslash found at the end of the previous
// line is not for a multiline property value.
if (line.isEmpty()
|| line.startsWith("#")
|| line.matches("^\\w+(\\.\\w+)*=")) {
result.append("\\\\");
}
}
// if a backslash is found at the end of the line remove it
// and decide what to do depending on the next line.
if (line.endsWith("\\")) {
endingBackslash = true;
line = line.substring(0, line.length() - 1);
} else {
endingBackslash = false;
}
result.append(line.replace("\\", "\\\\"));
}
if (endingBackslash) {
result.append("\\\\");
}
return new StringReader(result.toString());
}
private static Properties getProperties(File file) throws IOException {
Properties result = new Properties();
result.load(preparePropertyFile(file));
return result;
}
AlexS answered 2020-06-17T00:41:24Z
1 votes
如下将\替换为\\:
c:\sdjf\slkdfj.jpg
至
c:\\sdjf\\slkdfj.jpg
Dileep Dondapati answered 2020-06-17T00:41:48Z
0 votes
在属性文件中使用反斜杠并不是一件好事,因为它们是转义符。
尽管如此:Windows用户将倾向于在任何路径下使用它们...因此,在一行中感谢apache通用IO:
params.load(new StringReader(IOUtils.toString(paramFile.toURI(), null).replaceAll("\\\\", "/")));
Olivier Faucheux answered 2020-06-17T00:42:13Z
0 votes
以下代码将有所帮助:
BufferedReader metadataReader = new BufferedReader(new InputStreamReader(new FileInputStream("migrateSchemaGenProps.properties")));
Properties props = new Properties();
props.load(new StringReader(IOUtils.getStringFromReader(metadataReader).replace("\\", "/")));
Vaibhav Jain answered 2020-06-17T00:42:33Z