gpt4 book ai didi

java - .properties 文件中的反斜杠 (\) 被 Spring 的 'Environment' 变量忽略

转载 作者:行者123 更新时间:2023-12-02 04:22:50 27 4
gpt4 key购买 nike

我正在尝试使用 @PropertySourceEnvironment 变量在 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com