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.a
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());
}
You can use getKeys().
It returns an Iterator<String>
on all the keys in the properties file.
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
}