4

In my project we have a properties file which has some entries as below:

#Data key entries
datakey001
datakey321
datakey451
someotherkey

In fact its not a key value pair, but a list of keys. Using java.util.Properties I was able to get this using Properties.stringPropertyNames().

Now we are migrating to Apache Commons Configuration and I could not find any feature in this library to get all these keys as I used to get previously using java.util.Properties.

Is there any way in apache commons config by which I can get all these keys without changing the the structure of the properties file?

EDIT: I have tried using Configuration.getKeys() as below, but the output is empty.

 Configuration propertiesConfig = new PropertiesConfiguration("C:\\proj\\myprops.properties");
 Iterator<String> it = propertiesConfig.getKeys();
 while(it.hasNext()) {
     System.out.println(it.next());
 }
f_puras
  • 2,521
  • 4
  • 33
  • 38
jacobcs
  • 539
  • 5
  • 14

1 Answers1

2

You can use the Configuration.getKeys() method for that.

Gergely Bacso
  • 14,243
  • 2
  • 44
  • 64
  • Thanks Gergely! but that's something I had tried and I forgot to mention in the question. When I use Configuration.getKeys(), I get an empty iterator in this case. WIll update the question now. – jacobcs Aug 19 '15 at 06:03
  • 1
    That is strange. Just for testing purposes, could you add a few "=" signs to the end of your rows to see if that does the trick? Maybe your valueless config entries are treated as malformatted inputs. – Gergely Bacso Aug 19 '15 at 06:30
  • Yeah, when I add "=" sign I am able to fetch the keys. Looks like the entries are treated as malformatted inputs. Any ideas/workarounds that I can try at the code level? – jacobcs Aug 19 '15 at 06:44
  • No idea what to do on code level. Honestly I would just add those "=" signs everywhere. It is a one-minute-job on linux: sed 's/$/=/g' myprops.properties , or in any fancy Windows text-editor. – Gergely Bacso Aug 19 '15 at 06:59