简体   繁体   English

转义属性文件java中的特殊字符

[英]Escape special characters in properties file java

I have key, value in properties file like this proj.path=${HOME}/dir .我在属性文件中有 key, value 像这样proj.path=${HOME}/dir I have both environment variable and directory also with ${HOME} .我也有${HOME}环境变量和目录。

In my case I would like to use it as directory path only but when I read this from file it is getting replaced with environment variable value (home/user/dir) .在我的情况下,我只想将它用作目录路径,但是当我从文件中读取它时,它会被环境变量值(home/user/dir)替换。

I tried to escape it like proj.path=\\\\$\\\\{HOME\\\\}/dir but in code it is coming like \\$\\{HOME\\}/dir我试图像proj.path=\\\\$\\\\{HOME\\\\}/dir一样转义它,但在代码中它就像\\$\\{HOME\\}/dir

Required output is ${HOME}/dir .所需的输出是${HOME}/dir

EDIT:编辑:

Prop file:道具文件:

proj.path=${HOME}/dir,some/dir/dir2 proj.path=${HOME}/dir,some/dir/dir2

I am accessing in spring like below.我正在春天访问,如下所示。

@Value("#{'${proj.path}'.split(',')}") private List<String> customPaths;

One way of escaping the ${HOME} value is by wrapping the $ character in an expression and changing the type from List<String> to String[] .转义 ${HOME} 值的一种方法是将 $ 字符包装在表达式中并将类型从List<String>更改为String[]

proj.path=#{'$'}{HOME}/dir,some/dir/dir2

@Value("${proj.path}")
private String[] customPaths;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM