![](/img/trans.png)
[英]Java Custom Object with multiple properties as Map key or concatenation of its properties
[英]Java concatenation in properties file
我创建了一个名为 myproperties.properties 的属性文件:
test.value1=one
test.value2=two
我读取这个文件的java代码如下:
String test = Utility.getInstance().getProperty("test.value1");
其中类 Utility 是这样定义的:
public class Utility {
private static Utility _instance = null;
private static Properties properties = new Properties();
static public Utility getInstance(){
if (_instance == null) {
_instance = new Utility();
}
return _instance;
}
private Utility(){
loadUtility();
}
public String getProperty(String tgtPropertyName) {
Object prop = properties.get(tgtPropertyName);
if (prop != null) {
return prop.toString();
} else {
return null;
}
}
private void loadUtility(){
String filename = null;
try{
filename = getClass().getClassLoader().getResource("myproperties").getFile();
InputStream file = new FileInputStream(new File(filename));
properties.load(file);
Iterator iter = properties.keySet().iterator();
while (iter.hasNext()){
System.out.println("FILE LOADED");
}
}catch(Exception e){
}
}
}
此代码正常工作。 现在我必须在我的属性文件中添加一个连接:
test.value3=${test.value1}${test.value2}
这不起作用,因为我的 Java 代码无法解释 ${}。
例外是:
引起:java.lang.IllegalStateException:流处理程序不可用,原因是:对于输入字符串:“${test.value1}”
为什么?
使用以下代码连接属性文件中的 type.value3
Properties prop=null;
public FileReader FileLoader() throws FileNotFoundException
{
File file=new File("myproperties.properties");
FileReader fileReader=new FileReader(file);
return fileReader;
}
public String propertyLoader(String key) throws IOException
{
FileReader fileReader=FileLoader();
prop=new Properties();
prop.load(fileReader);
String value=prop.getProperty(key);
return value;
}
public void resultWriter() throws IOException
{
String value1=propertyLoader("test.value1");
String value2=propertyLoader("test.value2");
String res=value1+value2;
System.out.println(res);
FileWriter fw=new FileWriter("myproperties.properties");
prop=new Properties();
prop.setProperty("test.value3", res);
prop.store(fw, null);
}
public static void main(String[] args) throws IOException
{
UtilityNew util=new UtilityNew();
util.resultWriter();
System.out.println("Success");
}
核心 Java 不支持嵌套属性。 您唯一可以做的就是创建一个类,一旦您将 file.properties 加载到 Properties 对象中,该类将解析 ${XXX} 值。
或者类型安全库可能对您有用。 https://github.com/lightbend/config 。 它有很多功能,其中之一是替换:
替换(“foo”:${bar},“foo”:你好 ${who})
但是您将不再拥有键值属性文件,它看起来更像是一个 json 文件。
这可能是一个迟到的答案,但有人可能会发现这很有用。
您可以编写一个小的实用程序函数来读取属性值,然后迭代地替换存在的任何嵌套值首先搜索您的模式。 通过查找属性将其替换为实际值。 重复此操作,直到获得最终字符串。
Properties properties = new Properties();
properties.setProperty("base_url", "http://base");
properties.setProperty("subs_url", "${base_url}/subs");
properties.setProperty("app_download", "apps/download");
properties.setProperty("subs_detail", "${subs_url}/detail/${app_download}");
String input = properties.getProperty("subs_detail");
Pattern pattern = Pattern.compile("\\$\\{.*?\\}"); //change the pattern here to find nested values
while (pattern.matcher(input).find())
{
Matcher match = pattern.matcher(input);
while (match.find())
{
input = input.replace(match.group(), properties.getProperty(match.group().substring(2, match.group().length()-1)));
}
}
System.out.println("final String : " + input); // this prints http://base/subs/detail/apps/download
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.