How to read properties file in java

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.

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:

Reading from properties file
—————————–
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:

Reading from properties file
—————————–
host : localhost
username : java2blog
password : java123
—————————–

That’s all about how to read properties file in java

Related Posts

  • 27 December

    How to get current working directory in java

    Learn about how to get current working directory in java using different ways

  • 08 August

    Difference between Scanner and BufferReader in java

    In this post, we will see difference between Scanner and BufferReader in java. Java has two classes that have been used for reading files for a very long time. These two classes are Scanner and BufferedReader. In this post, we are going to find major differences and similarities between the two classes. Introduction Let’s first […]

  • 29 March

    Read UTF-8 Encoded Data in java

    In this post, we will see how to read UTF-8 Encoded Data. Sometimes, we have to deal with UTF-8 Encoded Data in our application. It may be due localization or may be processing data from user input. There are multiple ways to read UTF-8 Encoded Data in Java. Using Files’s newBufferedReader() We can use java.nio.file.Files's […]

  • 29 March

    Write UTF-8 Encoded Data in java

    In this post, we will see how to write UTF-8 Encoded Data. Sometimes, we have to deal with UTF-8 Encoded Data in our application. It may be due localization or may be processing data from user input. We will use Hindi language sentences to write in file. There are three ways to write UTF-8 Encoded […]

  • 15 March

    Java read file line by line

    In this post, we will see different ways to read file line by line in java. Sometimes, we need to read file line by line to a String, and process it. Here are different ways to read file line by line in java. Java 8 Streams Java 8 has introduced a new method called lines() […]

  • 06 February

    Java FileWriter Example

    The FileWriter class of java.io package makes writing to files in Java as easy as A-B-C. The FileWriter class writes to files as a stream of characters instead of bytes(as in FileOutputStream). Like its input-oriented counterpart FileReader, the FileWriter class is intended for writing “text” based files one character at a time. Let us look […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.