How to read a properties file in Java

In this tutorial we will demonstrate how to read a properties file in Java from your classpath.  Property files are generally loaded on application startup, to help initialise the application. Property files commonly contains information relating to external service endpoints, connection pool configurations etc …

How to read a properties file in Java

In this example we will be using the following properties file, which is normally located in the /main/resources directory of your project.

The following code example demonstrates how to load the property file using the ClassLoader. The ClassLoader will load the property file if it is present on the classpath.  If the property file is not on the classpath then no file will be read and your InputStream will be null.

Running the above code produces the following output:

I hope you find this post useful, and it helps you understand how to load a property file from your classpath.

Similar posts:

  1. Logback configuration examples
  2. Better application logging with MDC
  3. Java 9 JShell Basics

References:

  1. Java ClassLoader API Documentation

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *