How to get list of properties using apache.commons

前端 未结 3 1279
走了就别回头了
走了就别回头了 2021-02-14 02:48

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         


        
3条回答
  •  野的像风
    2021-02-14 03:26

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

    Using Apache Commons version <2.1:

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

    Edited for Apache Commons Version 2.1:

    List list = new ArrayList<>();
    Parameters params = new Parameters();
    FileBasedConfigurationBuilder builder =
        new FileBasedConfigurationBuilder
        (PropertiesConfiguration.class)
        .configure(params.properties()
        .setFileName("prop.properties"));
    try
    {
        Configuration config = builder.getConfiguration();
        Iterator keys = config.getKeys();
        while(keys.hasNext()){
          String key = (String) keys.next();
          list.add(key);
        }
    }
    catch(ConfigurationException cex)
    {
        // handle exception here
    }
    

    提交回复
    热议问题