How to read properties file in java

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

How to read properties file in java

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

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.

Here is the video tutorial of: "How to read properties file in Java?"

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