How to read properties file in Java

This post introduces how to read properties file in java. Property file is mainly used in Java related technologies to store the configurable parameters of an application. Beside, the properties file can also be used for storing strings for Internationalization and localization, etc. I make sure that you will see properties file in almost java project.

Each entry is stored as a pair of strings (call the name/value), one storing the name, and the other storing the value. Entries are generally expected to be a single line of the form, one of the following

Let’s create a sample program that demonstrate how to read properties file in java.

Create java project

In this how to read properties file in java tutorial, we create java project in eclipse with the final directory structure like below:

how to read properties file in java

Create properties file

We create properties file named myconfig.properties that contains content below:

Noticed that the myconfig.properties file will be stored in the resources directory(resources/config.properties) of java project. You can see it on the project directory structure.

Create PropertiesUtils class

You can also write the above code in main class. However, i like the way that package it in a separated class.

Create main class

To demonstrate how to read properties file in java , we create main class like this:

Run main class as Java application. We will get the ouput such as screen shot.

how to read properties file in java

That’s all on the how to read properties file in java tutorial.

+1
Shares 0

Leave a Comment

*

Please share it if you found this useful
Hide Buttons