This post will discuss how to print out all keys with values from a properties file in Java.

The properties file consists of a set of key-value pairs of string type, which can be loaded using the Properties class in Java. There are several ways to list all properties present in a properties file using the Properties class in Java:

1. Using keySet() method

Since Properties class extends java.util.Hashtable class, we can use its inherited keySet() method for iterating a Properties object.

The following code prints out the current set of system properties using the keySet() method, where the current system properties are loaded into a Properties object using the getProperties() method of the System class:

Download  Run Code

 
In Java 8, we can use Stream to print all system properties, as demonstrated below:

2. Using stringPropertyNames() method

We can also use the stringPropertyNames() method, which returns a set of keys present in the Properties object.

Download  Run Code

 
In Java 8, we can use Stream to list out all the properties, as shown below:

3. Using propertyNames() method

Alternatively, we can use the enumeration of all the keys returned by the propertyNames() method, as shown below:

Download  Run Code

4. Overriding put() method

Since Properties inherits from Hashtable, the put() method can be applied to a Properties object. The idea is to override the put() method and put each key-value pair into a map. Then we can simply iterate through the map to list out all the properties.

Download Code

5. Using Apache Commons Configuration

For reading the properties file in its original order, we can use third-party libraries like Apache Commons Configuration. Here’s a working example:

Download Code

That’s all about printing all keys with values from a properties file in Java.