简体   繁体   中英

String value from properties file

I have a properties (cant change this file) and it looks like:

aaa.bbb.ccc.first=my first value
aaa.bbb.ccc.second=my second value
aaa.bbb.ccc.third=my third value

If I need any value in java classes I use i18n.getText("aaa.bbb.ccc.first") but it works only for single value.

Problem is because I dont know:

-value's names

-how many values are in aaa.bbb.ccc.~

How is it possible to get list of value aaa.bbb.ccc~?

You could use a MapFilter . Use the MapFilter(Properties p, String prefix) constructor.

public void test() {
    Properties props = new Properties();
    props.put("aaa.bbb.ccc.first", "my first value");
    props.put("aaa.bbb.ccc.second", "my second value");
    props.put("aaa.bbb.ccc.third", "my third value");
    props.put("Other.props", "others");
    MapFilter<String> filtered = new MapFilter(props, "aaa.bbb.ccc.");
    for (Map.Entry<String, String> e : filtered.entrySet()) {
        System.out.println("Key: " + e.getKey() + " Value: " + e.getValue());
    }
    System.out.println(filtered);

}

Hash maps are not meant for the kind of lookup you want to do: Tries and radix trees are. There is an implementation of the Patricia trie data structure (ie, binary radix trees) in Apache Commons Collections : Just create a trie from your Map<String, Whatever> (you have a nice constructor at the purpose) and with prefixMap("aaa.bbb.ccc") you obtain the submap of all the entries whose key have that prefix.

Properties has a method propertyNames(). You can use that to get all the keys then do whatever you want from there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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