Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get list of properties using apache.commons

I need to get the list of properties which are in the .properties file. For example, if have the following .properties file:

users.admin.keywords = admin
users.admin.regexps = test-5,test-7
users.admin.rules = users.admin.keywords,users.admin.regexps

users.root.keywords = newKeyWordq
users.root.regexps = asdasd,\u0432[\u044By][\u0448s]\u043B\u0438\u0442[\u0435e]
users.root.rules = users.root.keywords,users.root.regexps,rules.creditcards

users.guest.keywords = guest
users.guest.regexps = *
users.guest.rules = users.guest.keywords,users.guest.regexps,rules.creditcards

rules.cc.creditcards = 1234123412341234,11231123123123123,ca
rules.common.regexps = pas
rules.common.keywords = asd

And as a result I'd like to get an ArrayList which consists of names of fields like this: users.admin.keywords, users.admin.regexps, users.admin.rules and so on. And as you have noticed, I need to do this using apache.commons.config

like image 347
Nikitin Mikhail Avatar asked Apr 23 '13 12:04

Nikitin Mikhail


People also ask

How can I get a list of properties file?

We can load a list from a properties file by annotating the field with the @Value annotation. And providing the property name of the resource. The PropertySourcesPlaceholderConfigurer is used to resolve the ${} in the expression. We can manipulate the property with the split() method.

What is the use of Apache Commons?

The Apache Commons is a project of the Apache Software Foundation, formerly under the Jakarta Project. The purpose of the Commons is to provide reusable, open source Java software. The Commons is composed of three parts: proper, sandbox, and dormant.

What is Apache Commons configuration?

Apache Commons Configuration is a java library that simplifies managing application configuration properties. It allows you to collect properties from different configuration sources like properties files, XML files, Java System properties, Environemnt variables etc.


3 Answers

You can use as below:

Configuration configuration = new PropertiesConfiguration(filename);
Iterator<String> keys = configuration.getKeys();
List<String> keyList = new ArrayList<String>();
while(keys.hasNext()) {
    keyList.add(keys.next());
}
like image 195
Apurv Avatar answered Nov 01 '22 11:11

Apurv


Properties prop = new Properties();
prop.load(new FileInputStream("prop.properties"));
Set<Map.Entry<Object, Object>> set = prop.entrySet();
List<Object> list = new ArrayList<>();
for (Map.Entry<Object, Object> entry : prop.entrySet())
{
   list.add(entry.getKey());
}
System.out.println(list);

Using Apache Commons version <2.1:

Configuration config = new PropertiesConfiguration("prop.properties");
List<String> list = new ArrayList<>();
Iterator<String> keys = config.getKeys();
while(keys.hasNext()){
    String key = (String) keys.next();
    list.add(key);
}

Edited for Apache Commons Version 2.1:

List<String> list = new ArrayList<>();
Parameters params = new Parameters();
FileBasedConfigurationBuilder<FileBasedConfiguration> builder =
    new FileBasedConfigurationBuilder<FileBasedConfiguration>
    (PropertiesConfiguration.class)
    .configure(params.properties()
    .setFileName("prop.properties"));
try
{
    Configuration config = builder.getConfiguration();
    Iterator<String> keys = config.getKeys();
    while(keys.hasNext()){
      String key = (String) keys.next();
      list.add(key);
    }
}
catch(ConfigurationException cex)
{
    // handle exception here
}
like image 26
AllTooSir Avatar answered Nov 01 '22 10:11

AllTooSir


You can use getKeys().

It returns an Iterator<String> on all the keys in the properties file.

like image 40
Jean Logeart Avatar answered Nov 01 '22 09:11

Jean Logeart