Property file is a typical plain java file with key and value pairs. It will have an extension of .properties. ¬†Usually system level configurations or server level configuration. Each line typically refers to a configurable parameter. Format looks like key=value . Key being the Property name and value being the key’s Value. This is the simple Properties file¬† used in our sample program.

AuthorInfo.properties

Author=Viswa Teja Swarup Reddy
Website=www.DevelopProjects.com
Language=English

Example Program to Read a Property file in Java

package com.developprojects.java;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Properties;
import java.util.Set;

public class ReadPropertyFile {

 public static void main(String args[]) {
 readPropertyFile();
 }

 public static void readPropertyFile() {
 System.out.println("In readPropertyFile method");
 Properties prop = new Properties();
 InputStream input;
 HashMap<String, String> propvals = new HashMap<String, String>();
 try {

 input = ReadPropertyFile.class
 .getResourceAsStream("/AuthorInfo.properties");
 prop.load(input);
 System.out.println("Property File Loaded Succesfully");
 Set<String> propertyNames = prop.stringPropertyNames();
 for (String Property : propertyNames) {
 System.out.println(Property + ":" + prop.getProperty(Property));
 propvals.put(Property, prop.getProperty(Property));
 }
 System.out.println("HashMap generated::" + propvals);
 } catch (FileNotFoundException e) {
 e.printStackTrace();
 } catch (IOException e) {
 e.printStackTrace();
 } catch (Exception e) {
 e.printStackTrace();
 }
 }
}

output:

In readPropertyFile method
Property File Loaded Succesfully
Language:English
Website:www.DevelopProjects.com
Author:Viswa Teja Swarup Reddy
HashMap generated::{Website=www.DevelopProjects.com, Author=Viswa Teja Swarup Reddy, Language=English}

Explanation:

Properties prop = new Properties() creates a new Properties Object.

HashMap<String, String> propvals = new HashMap<String, String>() creates a propvals HashMap Object.

readPropertyFile() is the custom function which we have written to read the property file in the class path.

The method getResourceAsStream returns an InputStream. By calling this method with class loader, it will search the resource in the class path.

Load method will read the key value pairs from the given input stream.

The rest of the program is just iterating the elements in property file.

stringPropertyNames() returns a Set containing all the property keys.

We iterated the set to get value of the corresponding key using the method, getProperty(<propertyName>).

We are just storing the key value pairs just in the same order into our HashMap.

In the next Program, I showed you how to write into a property file.



Swarup Reddy (13 Posts)

Founder of Developprojects.com. He is an Oracle certified expert java developer. He is proficient in end-to-end development right from requirement analysis to system study, designing, coding, testing, de- bugging, documentation and implementation. His areas of interests are Information Security, Programming, compiler designing and automation.