How to read properties file in java

Properties files are used in java projects to externalise configuration, for example, database settings. In this post , we will see how to read properties file in java.

Java used Properties class to store above key values pair. Properties.load method is very convenient to load properties file in form of key values pairs.

Properties file looks something like this.

There are two ways you can do it.

  • Read properties file from system
  • Read properties file  from classpath

Read properties file from System :

In this, you need to read properties file from system path. Here I am putting properties file in root level of project.

java code:

When you run above program, you will get following output:

Read properties file from classpath: 

You can read properties file to classpath too. you have $project/src as default classpath as this src folder will be copied to classes. You can put it in $project/src folder and read it from there.

you need to use

to read it from classpath.

When you run above program, you will get following output:

Add Comment