Print all keys with values from a properties file in Java
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.Properties; class Main { public static void main(String[] args) { Properties prop = System.getProperties(); printProperties(prop); } public static void printProperties(Properties prop) { for (Object key: prop.keySet()) { System.out.println(key + ": " + prop.getProperty(key.toString())); } } } |
In Java 8, we can use Stream to print all system properties, as demonstrated below:
|
1 2 3 4 5 |
public static void printProperties(Properties prop) { prop.keySet().stream() .map(key -> key + ": " + prop.getProperty(key.toString())) .forEach(System.out::println); } |
2. Using stringPropertyNames() method
We can also use the stringPropertyNames() method, which returns a set of keys present in the Properties object.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.Properties; class Main { public static void main(String[] args) { Properties prop = System.getProperties(); printProperties(prop); } public static void printProperties(Properties prop) { for (String key: prop.stringPropertyNames()) { System.out.println(key + ": " + prop.getProperty(key)); } } } |
In Java 8, we can use Stream to list out all the properties, as shown below:
|
1 2 3 4 5 |
public static void printProperties(Properties prop) { prop.stringPropertyNames().stream() .map(key -> key + ": " + prop.getProperty(key)) .forEach(System.out::println); } |
3. Using propertyNames() method
Alternatively, we can use the enumeration of all the keys returned by the propertyNames() method, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Enumeration; import java.util.Properties; class Main { public static void main(String[] args) { Properties prop = System.getProperties(); printProperties(prop); } public static void printProperties(Properties prop) { Enumeration e = prop.propertyNames(); while (e.hasMoreElements()) { String key = e.nextElement().toString(); System.out.println(key + ": " + prop.getProperty(key)); } } } |
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.Map; import java.util.Properties; class Main { public static void main(String[] args) { String propertiesFilename = "config.properties"; // load from the filesystem Map<String, String> properties = new HashMap<>(); try (InputStream stream = new FileInputStream(propertiesFilename)) { Properties prop = new Properties() { @Override public synchronized Object put(Object key, Object value) { return properties.put(key.toString(), value.toString()); } }; prop.load(stream); } catch (IOException e) { e.printStackTrace(); } System.out.println(properties); } } |
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.PropertiesConfiguration; import org.apache.commons.configuration.PropertiesConfigurationLayout; import java.util.Set; class Main { public static void main(String[] args) { String propertiesFilename = "config.properties"; // load from the classpath PropertiesConfiguration config = null; try { config = new PropertiesConfiguration(propertiesFilename); } catch (ConfigurationException e) { e.printStackTrace(); } PropertiesConfigurationLayout layout = config.getLayout(); printProperties(layout); } public static void printProperties(PropertiesConfigurationLayout layout) { Set<String> keys = layout.getKeys(); for (String key: keys) { String value = layout.getConfiguration().getProperty(key).toString(); System.out.println(key + ": " + value); } } } |
That’s all about printing all keys with values from a properties file in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)