作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 @PropertySource
和 Environment
变量在 Spring @Configuration
java 类中加载 config.proprties 文件数据。
示例:http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/PropertySource.html问题是,我有一个具有如下值的属性:
serverName = abc\xyz
当我使用方法读取此属性时,
String server= env.getProprty("serverName");
System.out.print(server);
值打印为“abcxyz”。
请注意,我尝试使用双反斜杠,
serverName = abc\\xyz
但它仍然只是忽略值字符串中的\。另外,我不能使用正斜杠代替反斜杠。
你能帮我解决这个问题吗?提前致谢!!
最佳答案
我没有使用反斜杠,而是使用正斜杠将它们存储在配置文件中。
在阅读时,我用双反斜杠替换了它们。
SourcePath=C:/Users/Barani/Documents/SampleData/MyInputFile.txt
Path currentRelativePath = Paths.get("");
String filePath = currentRelativePath.toAbsolutePath().toString() + "/config.properties";
Properties props = new Properties();
FileInputStream fis = new FileInputStream(filePath);
props.load(fis);
sourcePath = props.getProperty("SourcePath").replace("/", "\\\\");
这对我来说是正确的。
关于java - .properties 文件中的反斜杠 (\) 被 Spring 的 'Environment' 变量忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37858207/
我是一名优秀的程序员,十分优秀!