Java Collection How to - Get a key List from Properties








Question

We would like to know how to get a key List from Properties.

Answer

propertyNames() method in Properties class combines the keys in the properties list with the keys in the defaults properties list passed into the constructor.

import java.io.FileInputStream;
import java.util.Enumeration;
import java.util.Properties;
/*from w  ww.j a v  a  2s  . c o m*/
public class MainClass {
  public static void main(String args[]) throws Exception {
    Properties p = new Properties();
    p.load(new FileInputStream("test.txt"));
    Enumeration e = p.propertyNames();

    for (; e.hasMoreElements();) {
      System.out.println(e.nextElement());

    }
  }
}

The code above generates the following result.

The code above generates the following result.