Read properties files in Java
This post will discuss how to read properties files in Java.
A properties file consists of key-value pairs of string types that can have any extension, although .properties is recommended to distinguish them from other files easily.
We can read properties files in Java using the Properties class. The Properties class represents a persistent set of properties that can be loaded from a stream using its load() method.
1. Using ClassLoader
The class loader is responsible for loading classes and resources. We can use the ClassLoader class to load the properties file from the classpath in a static context, as shown below:
|
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 33 34 35 36 37 38 39 40 |
import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Properties; class Main { private static Properties loadProperties(String propertiesFilename) { Properties prop = new Properties(); // ClassLoader loader = Thread.currentThread().getContextClassLoader(); ClassLoader loader = Main.class.getClassLoader(); try (InputStream stream = loader.getResourceAsStream(propertiesFilename)) { if (stream == null) { throw new FileNotFoundException(); } prop.load(stream); } catch (IOException e) { e.printStackTrace(); } return prop; } public static void main(String[] args) { String propertiesFilename = "config.properties"; // on classpath Properties prop = loadProperties(propertiesFilename); printProperties(prop); } public static void printProperties(Properties prop) { prop.stringPropertyNames().stream() .map(key -> key + ":" + prop.getProperty(key)) .forEach(System.out::println); } } |
Every Class object contains a reference to the ClassLoader that defined it. In a non-static context, we can do something like the following to load the properties file from the classpath using the ClassLoader class.
|
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 33 34 35 36 37 38 |
import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Properties; class Main { private Properties loadProperties(String propertiesFilename) { Properties prop = new Properties(); ClassLoader loader = this.getClass().getClassLoader(); try (InputStream stream = loader.getResourceAsStream(propertiesFilename)) { if (stream == null) { throw new FileNotFoundException(); } prop.load(stream); } catch (IOException e) { e.printStackTrace(); } return prop; } public static void main(String[] args) { String propertiesFilename = "config.properties"; // on classpath Properties prop = new Main().loadProperties(propertiesFilename); printProperties(prop); } public static void printProperties(Properties prop) { prop.stringPropertyNames().stream() .map(key -> key + ":" + prop.getProperty(key)) .forEach(System.out::println); } } |
If our resource exists on the file system rather than on the classpath, we can use the static method getSystemResourceAsStream() of the ClassLoader class, which returns an InputStream for reading the resource.
|
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 33 34 35 36 37 38 39 40 |
import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Properties; class Main { private static Properties loadProperties(String propertiesFilename) { Properties prop = new Properties(); try (InputStream stream = ClassLoader.getSystemResourceAsStream(propertiesFilename)) { if (stream == null) { throw new FileNotFoundException(); } prop.load(stream); } catch (IOException e) { e.printStackTrace(); } return prop; } public static void main(String[] args) { // not on the classpath (file system) String propertiesFilename = "config.properties"; Properties prop = loadProperties(propertiesFilename); printProperties(prop); } public static void printProperties(Properties prop) { prop.stringPropertyNames().stream() .map(key -> key + ":" + prop.getProperty(key)) .forEach(System.out::println); } } |
2. Using Class
We can use getResourceAsStream() method of the Class object instead of ClassLoader class. This method delegates to this object’s class loader.
|
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 33 34 35 36 37 |
import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Properties; class Main { private Properties loadProperties(String fileName) { Properties prop = new Properties(); try (InputStream stream = this.getClass().getResourceAsStream(fileName)) { if (stream == null) { throw new FileNotFoundException(); } prop.load(stream); } catch (IOException e) { e.printStackTrace(); } return prop; } public static void main(String[] args) { String propertiesFilename = "config.properties"; // on classpath Properties prop = new Main().loadProperties(propertiesFilename); printProperties(prop); } public static void printProperties(Properties prop) { prop.stringPropertyNames().stream() .map(key -> key + ":" + prop.getProperty(key)) .forEach(System.out::println); } } |
3. Using FileInputStream
If your properties file exists on the file system rather than on classpath, we can use FileInputStream to get an InputStream, as shown below:
|
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 33 34 35 36 37 38 39 40 |
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Properties; class Main { private static Properties loadProperties(String propertiesFilename) { Properties prop = new Properties(); try (InputStream stream = new FileInputStream(propertiesFilename)) { if (stream == null) { throw new FileNotFoundException(); } prop.load(stream); } catch (IOException e) { e.printStackTrace(); } return prop; } public static void main(String[] args) { // not on the classpath (file system) String propertiesFilename = "config.properties"; Properties prop = loadProperties(propertiesFilename); printProperties(prop); } public static void printProperties(Properties prop) { prop.stringPropertyNames().stream() .map(key -> key + ":" + prop.getProperty(key)) .forEach(System.out::println); } } |
4. Using Apache Commons Configuration
We can even read properties file using 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 |
import org.apache.commons.configuration.Configuration; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.PropertiesConfiguration; class Main { public static void main(String[] args) { String propertiesFilename = "config.properties"; Configuration config = null; try { config = new PropertiesConfiguration(propertiesFilename); } catch (ConfigurationException e) { e.printStackTrace(); } System.out.println(config.getString("someKey")); } } |
5. Using ResourceBundle
Finally, we can use the ResourceBundle class to read the properties file, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.ResourceBundle; class Main { public static void main(String[] args) { String propertiesFilename = "config"; ResourceBundle rb = ResourceBundle.getBundle(propertiesFilename); printProperties(rb); } public static void printProperties(ResourceBundle rb) { rb.keySet().stream() .map(key -> key + ":" + rb.getString(key)) .forEach(System.out::println); } } |
We can also load the properties files into a FileInputStream, and then use a PropertyResourceBundle to get ResourceBundle instance, as demonstrated below:
|
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 java.io.FileInputStream; import java.io.IOException; import java.util.PropertyResourceBundle; import java.util.ResourceBundle; class Main { private static ResourceBundle loadResource(String propertiesFilename) throws IOException { FileInputStream fis = new FileInputStream(propertiesFilename); return new PropertyResourceBundle(fis); } public static void main(String[] args) { String propertiesFilename = "config.properties"; ResourceBundle rb = null; try { rb = loadResource(propertiesFilename); } catch (IOException e) { e.printStackTrace(); } printProperties(rb); } public static void printProperties(ResourceBundle rb) { rb.keySet().stream() .map(key -> key + ":" + rb.getString(key)) .forEach(System.out::println); } } |
That’s all about reading property files 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 :)