ldap server time

How to Read Attribute Values from a Properties file in Java?

Properties file stores configurable parameters mostly in key value pairs. Learn more about properties file from here. Here we will learn how to fetch values from a properties file in java. Since as a part of standardization we have been said many times that we should not hard code values to any field and instead extract it from somewhere. The use of properties file can vary from business code to code. One can simply use the properties file to retrieve different languages for their website. So, let us at first have a quick look at the code responsible for retrieving values from a properties file. In the below code we have created a class named ReadProperty in which we will have our function written. Now in order to create properties file right click on the folder in which you want the file to be present->new->file. Name the file as somename.properties.

import java.io.InputStream;
import java.util.Properties;

public class ReadProperty {
public String getValue(String key, String domain) throws Exception {

String returnServerPath = null;
try {
Properties prop = new Properties();
InputStream serverStream = this.getClass().getResourceAsStream(
"/resource/login_" + domain + ".properties");
returnServerPath = (String) prop.get(key);

System.out.println("In read property server path"
+ returnServerPath);
} catch (Exception ex) {


return returnServerPath;

So here we are using the getResourceAsStream() method to retrieve the input stream to read any specified resource. Post this with the help of the key we are retrieving it's corresponding value. Remember that the values in properties file are stored as key-value pair. In the getResourceAsStream() method we are using the exact format in which we have our properties file name, login_formulaestack.properties and resource is the package name. Below are the two attributes we are retrieving.

login=submit button

Coming to our JSP page:

<%String domain="formulaestack"; ReadProperty rp=new ReadProperty(); %></pre>
<form class="form-horizontal" role="form" action="#" autocomplete="off" method="post">&nbsp;

<h2><label class="label label-default"><%= rp.getValue("Uname",domain)%></label></h2>

<div class="col-lg-9"><input id="username" class="form-control" required="" type="text" placeholder="Username" /></div>
<div class="form-group">

<div class="col-sm-offset-3 col-sm-9"><button class="btn btn-success btn-sm" type="button"><%= rp.getValue("login",domain)%></button></div>



In our JSP page, we are creating the instance of the ReadProperty class in order to invoke the getValue() method and setting the domain to formulaestack in the scriptlet. Next, we invoke the getValue() using the key as present in the properties file and on displaying the same we will be able to view the result. Let us see the output.


properties file

That's it, Hurray we are done!! If you liked it share & comment below. And don't forget to read our other posts.

Leave a comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.