简体   繁体   English

根据键值从属性文件中有选择地加载属性

[英]Load properties selectively from properties file based on key value

I have a property file which has values like 我有一个属性文件,其值如下

xxx.key1 = value1
xxx.key2 = value2

yyy.key3 = value3
yyy.key4 = value4

'xxx' and 'yyy' can be considered as 2 different namespaces. 可以将'xxx''yyy'视为2个不同的名称空间。 How do I load property file so that I can only load either property of 'xxx' or 'yyy' ? 如何加载属性文件,以便只能加载'xxx''yyy'属性?

Just read each line of the file and only pull back the values that match that namespace. 只需读取文件的每一行,然后仅拉回与该名称空间匹配的值即可。

Scanner scan = new Scanner(new File("yourfilepath"));
Map<String,String> map = new HashMap<String, String>();
String value = "";
while(scan.hasNext())
{
    value = scan.nextLine();
    if(value.indexOf("xxx") != -1)
    {
        map.put(value.split(" = ")[0], value.split(" = ")[1]);
    }
}

//now map has your key value pairs

If your properties are like this 如果你的财产是这样的

xxx.key1=value then in the split("=") (no spaces) xxx.key1 = value,然后在split(“ =”)中(无空格)

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

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