Home Java Example Java Util Properties How to read properties file in Java?
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

How to read properties file in Java?

Advertisement
In this tutorial you will learn "How to read properties file in Java?". The data of the property file is displayed on the console.

Example program of reading a properties file and then printing the data on console

In this video tutorial I will show you how to use the java.util.Properties class for reading a property file in Java program

The java.util.Properties class is subclass of the Hashtable, which is used to map the keys to value.

The java.util.Properties class is used to read the properties file. The properties files are simple text file which is saved as .properties extensions.

Here is example of property file:

name=Rose India
address=Rohini, Delhi

Properties file contains data in the key and value pair.

The getProperty() method of the Properties class is used to read the value of the properties file.

Here is the video tutorial of reading a property file in Java:

Here is the data.properties the properties file used in this example:

name=Rose India
address=Rohini, Delhi

Here is the source code of the Java program(ReadPropertiesFile.java):

package net.roseindia;

import java.io.*;
import java.util.*;

public class ReadPropertiesFile {
	public static void main(String[] args) {
		Properties prop = new Properties();
		try {
			prop.load(new FileInputStream("./data.properties"));
			String name = prop.getProperty("name");
			String address = prop.getProperty("address");
			System.out.println("Name: " + name);
			System.out.println("Address: " + address);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
}

Above program displays the value of the keys "name" and "address" on the console.

You can find more tutorials of Java Properties class in our Java properties files tutorials section.

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: May 12, 2014

Ask Questions?    Discuss: How to read properties file in Java?  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com