Java Properties反斜杠 [英] Java Properties backslash
问题描述
我正在使用Java Properties来读取属性文件。一切都工作正常,但属性默默地删除反斜杠。
I am using Java Properties to read a properties file. Everything is working fine, but Properties silently drops the backslashes.
(即)
original: c:\sdjf\slkdfj.jpg
after: c:sdjfslkdfj.jpg
如何让属性不这样做?
我使用代码 prop.getProperty(key )
我从文件中获取属性,我想避免添加双反斜杠
I am getting the properties from a file, and I want to avoid adding double backslashes
推荐答案
Properties.load()导致您看到反斜杠的问题用于特殊目的。
It is Properties.load() that's causing the problem that you are seeing as backslash is used for a special purpose.
保存关键元素对的所有数据
的逻辑行可以通过转义行终止符在几个相邻的自然
行中传播
带有反斜杠字符的
序列,
\\。
The logical line holding all the data for a key-element pair may be spread out across several adjacent natural lines by escaping the line terminator sequence with a backslash character, \.
如果您无法使用CoolBeans的建议,那么您可以做的是事先将属性文件读取为字符串并替换反斜杠使用双反斜杠,然后将其提供给Properties.load()
If you are unable to use CoolBeans's suggestion then what you can do is read the property file beforehand to a string and replace backslash with double-backslash and then feed it to Properties.load()
String propertyFileContents = readPropertyFileContents();
Propertied props = new Properties();
props.load(new StringReader(propertyFileContents.replace("\\","\\\\"));
这篇关于Java Properties反斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!