In this post , we will see how to read properties file in java.
Properties
files are used in java projects to externalise configuration, for example, database settings.
Java uses Properties
class to store the 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.
Table of Contents [hide]
Read properties file from System
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:
—————————–
host : localhost
username : java2blog
password : java123
—————————–
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 this.getClass().getResourceAsStream("/config.properties");
to read it from classpath
.
When you run above program, you will get following output:
—————————–
host : localhost
username : java2blog
password : java123
—————————–
That’s all about how to read properties file in java