Monday, March 6, 2017

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

Here 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.

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.

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.