Java属性反斜杠

我正在使用Java属性来读取属性文件。 一切工作正常,但属性默默滴下反斜杠。

(即)

original: c:\sdjf\slkdfj.jpg after: c:sdjfslkdfj.jpg 

我如何让属性不这样做?

我正在使用代码prop.getProperty(key)

我从文件中获取属性,我想避免添加双反斜杠

它是Properties.load() ,导致您看到的反斜杠用于特殊用途的问题。

保存关键元素对的所有数据的逻辑行可以通过用反斜线字符\来逃避行终止符序列而分散在几个相邻的自然行上。

如果您无法使用CoolBeans的build议,那么您可以事先将属性文件读取到一个string,并用双反斜杠replace反斜杠,然后将其提供给Properties.load()

 String propertyFileContents = readPropertyFileContents(); Propertied props = new Properties(); props.load(new StringReader(propertyFileContents.replace("\\","\\\\")); 

使用双反斜杠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 

UPDATE CREDIT to @ewh below。 显然,Windows可以识别正斜线。 所以我想你可以让你的用户用正斜杠来写,而如果你之后需要反斜杠,你可以做一个replace。 我testing了这个片段,它工作正常。

  Properties props = new Properties(); props.setProperty("test", "C:/dev/sdk/test.dat"); System.out.println(props.getProperty("test"));//prints C:/dev/sdk/test.dat 

使用斜杠。 Java中不需要在文件名中使用反斜杠。

如果你真的需要一个将被加载的属性文件中的反斜杠(比如对于不是文件path的属性), 为每个反斜杠字符加上\u005c

在@unhillbilly提供的文档中指出,反斜杠在属性文件中被特别处理。

@EJP:例如,如果要将NTLMlogin标识存储在属性文件(其格式为带有反斜杠的DOMAIN\USERNAME ,则肯定需要反斜杠。 这种types的属性不是一个文件名,所以正斜杠将无法正常工作。

编辑:@Max Nanasy:从上面提到的文件( java.util.Properties load javadoc )(强调我的)

该方法不会将无效转义字符之前的反斜杠字符“\”视为错误; 反斜杠被无声地丢弃 。 例如,在Javastring中,序列“\ z”会导致编译时错误。 相反,这种方法默默地放下了反斜杠。 因此,该方法将两个字符序列“\ b”视为等同于单个字符“b”

对于我来说,除非我指定unicode,否则在属性文件中反斜杠总是有问题(即使是双反斜杠'\\')。

除了Bala R的回答之外,我还有下面的解决scheme,以便在行尾保持反斜杠的新行语义。

这是我的代码:

 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; } 

在属性文件中使用反斜杠并不是一件好事,因为它们是转义字符。

尽pipe如此:一个Windows用户将趋势使用他们的任何path…因此,在一个单一的线,感谢Apache通用的IO:

 params.load(new StringReader(IOUtils.toString(paramFile.toURI(), null).replaceAll("\\\\", "/"))); 

以下代码将有助于:

 BufferedReader metadataReader = new BufferedReader(new InputStreamReader(new FileInputStream("migrateSchemaGenProps.properties"))); Properties props = new Properties(); props.load(new StringReader(IOUtils.getStringFromReader(metadataReader).replace("\\", "/"))); 

\\replace\如下所示:

 c:\sdjf\slkdfj.jpg 

 c:\\sdjf\\slkdfj.jpg