Java 属性反斜杠 [英] 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:sdjfslkdfj.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();
Properties properties = new Properties();
properties.load(new StringReader(propertyFileContents.replace("\", "\\")));
这篇关于Java 属性反斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!