Home Java Example Java Core How to read properties file in java
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

How to read properties file in java

Advertisement
We are going to describe how to read properties file in java.

How to read properties file in java

Description of Example:-

In this example we have used Properties class. This Properties class permit us to save data to a stream or load from a stream using the properties file. This properties file, stored data as a key value pair in the form of string.

It has two method read properties file;-

  • ResourceBundle.
  • Properties Class.  
  1.   load() method:- reads a property list (key and element matches) from the input stream.
  2.   getProperty():- method:-Searches for the property with the assigned key in the properties data file.

How we will create .properties file in directory. Fist of all we create myfile.Properties name then we will write value in properties files. You can see below:

fname= Naulej

lname= Kumar

address= Delhi

mobileno= 8285279608

Example of Read properties file in java

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

    public class PropertyFile {
	public static void main(String[] args) {
		   Properties file= new Properties();
		       try {
			file.load(new FileInputStream("c://myfile.properties"));
		     
                     String fname = file.getProperty("fname");
	             String lname = file.getProperty("lname");
	             String address = file.getProperty("address");
	             String mobileno = file.getProperty("mobileno");
	             System.out.println("FistName: " + fname);
	             System.out.println("LastName: " + lname);
	             System.out.println("Address: " + address);
	             System.out.println("Mobile No: " + mobileno);
			} catch (IOException e) {
				e.printStackTrace();
			}
	}
}

Output

Download Source Code

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: July 26, 2013

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