Tuesday, March 6, 2012

How to read Properties File in Java – XML and Text Example Tutorial

Reading and writing properties file in Java is little different than reading or writing text file in Java or  reading xml files in Java using xml parsers like DOM because Java provides special provision to properties file. For those who are not familiar with Properties file in java, It is used to represent configuration values like JDBC connectivity parameter or user preferences settings and has been a primary source of injecting configuration on Java application.  Properties file in Java is a text file which stores data in form of key and value, key being known as property. Java allows you to read value of property by providing its name which makes it easy to access configuration settings. Two popular example of properties file are jdbc.properties often used to store database connection settings like URL, username and password and log4j.properties file which settings for java logging using log4j. There are also many frameworks which uses Java properties file like Struts, Spring, Displaytag etc. Another advantage of using properties file is that you can represent data either in xml format or in properties format. xml format will be particularly useful if you are sharing configuration or settings with some external tool which understands only xml. In this article we will see how to read and write into Properties file in Java on both xml and text format.

Reading values from Java Properties File

example of How to read Properties File in Java – XML and Text formatHere is sample code example of reading a properties file in Java. In this example we will read property from jdbc.properties file which contains database connection settings , you might have already used that. Anyway it contains username, password and jdbc driver class name as key value format. We will read these values from Java program using java.util.Properties class which represent properties file in Java program.

jdbc.username=test
jdbc.password=unknown
jdbc.driver=com.mysql.jdbc.Driver

Code Examples of Reading Properties File in Java


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

public class PropertyFileReader {

    public static void main(String args[]) throws FileNotFoundException, IOException {

        //Reading properties file in Java example
        Properties props = new Properties();
        FileInputStream fis = new FileInputStream("c:/jdbc.properties");
     
        //loading properites from properties file
        props.load(fis);

        //reading proeprty
        String username = props.getProperty("jdbc.username");
        String driver = props.getProperty("jdbc.driver");
        System.out.println("jdbc.username: " + username);
        System.out.println("jdbc.driver: " + driver);

    }
}

Output:
jdbc.username: test
jdbc.driver: com.mysql.jdbc.Driver


If you read any property which is not specified in properties file than you will props.getProperty() will return null. Now let's see another example of reading property file from xml format. as you know properties file in java can be represented in xml format and Java provides convenient method called loadFromXML() to load properties from xml file. here is a quick example of parsing xml properties file and reading data:

How to read Property file in XML format – Java

In earlier section we have seen sample working code example of reading properties file (.properties) but as I said earlier you can also define property in xml format this method is also very popular among various Java logging framework e.g. log4j and and also among others. In this section we will see how to read property file which is written in xml format. If you see the code not many changes instead of  Properties.load() we are using Properties.loadFromXML() and than rest of stuff of getting property and printing its value is same as in last example.

By the way here is our sample java properties file in xml format, which defined two entries with key jdbc.username and jdbc.password.


<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
   <properties> <entry key="jdbc.username">root</entry>
        <entry key="jdbc.password">mysql</entry>
   </properties>

And here is Java program which will read XML properties file in Java:

public static void main(String args[]) throws FileNotFoundException, IOException {

        //Reading properties file in Java example
        Properties props = new Properties();
        FileInputStream fis = new FileInputStream("c:/properties.xml");
     
        //loading properites from properties file
        props.loadFromXML(fis);

        //reading proeprty
        String username = props.getProperty("jdbc.username");
        System.out.println("jdbc.username: " + username);
     
}

output:
jdbc.username: root


We have seen how to read properties file in java, In both text and xml format. Properties file are immensely helpful for providing settings and configuration data to any Java program. Text properties file can only represent linear values but xml properties file can also represent hierarchical values which makes Properties file preferred choice in logging frameworks.

Other Java tutorial you may like

6 comments :

Patrick P said...

Shouldn't your example include the use of the class loader? That seems to be the way many/most applications find and load the properties files.

Javin @ singleton questions java said...

Hi Patrick, Sorry I couldn't get your point. Can you elaborate more, are you saying loading properties file in static initializer block ?

Sujata said...

In order to load or read properties file i Java , properties file must be in classpath otherwise you will not be able to read properties.

Java Srilankan Support said...

Hopefully you will find the good tutorial for How to read Properties File in Java which will help you to get database connection parameters from the property file.

How to read Properties File in Java

Javin @ display tag example java said...

Thanks Java Srilankan support for liking this Java tutorial on properties file and sharing your How to read properties file in Java tutorial.If you Java tutorial here please consider sharing in your blog and group. keep in touch

Javin

Anonymous said...

how do i load a property file from relative path.here you have user static path, but what if it is a relative path like different folder within the project folder?

Post a Comment