Sunday, 28 July 2013

How to load and read the Property file from classpath in java?


Loading and Reading the Property file from classpath in java

Properties files are the important resources to store information in Java. These files are used to store static information as key and value pair.



There are ways to Load the property file in java.

1. With the help of getClassLoader().getResourceAsStream(fileName)
2. With the help of getClass().getResourceAsStream(fileName) 

In the below example, we will use above both the methods to load the properties files from the classpath:-

Content of the Test.properties file:-

NAME=KUMAR GAURAV
EMP_ID=1234
ORG_NAME=INDIA COMPANY


A program to load and read the property file from classpath.

package com.gaurav.loadpropertyfile;

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

public class PropertyFileLoader {
            /**
             * This method is responsible to load a property file.
             *
             * @return Properties
             */
            public static Properties loadPropertyFile(String fileName) {
                        Properties prop = null;
                        try {
                                    prop = new Properties();
                                    InputStream in = PropertyFileLoader.class.getClassLoader()
                                                            .getResourceAsStream(fileName);
                                    prop.load(in);
                                    in.close();
                        } catch (IOException ioe) {
                                    System.out.println(ioe.getMessage());
                        }
                        return prop;
            }

            /**
             * This method is responsible to load a property file without using getClassLoader() method.
             *
             * @return Properties
             */
            publicProperties loadPropertyFileUsingSecondWay(String fileName) {
                        Properties prop = null;
                        try {
                                    prop = new Properties();
                                    InputStream in = this.getClass().getResourceAsStream(fileName);
                                    prop.load(in);
                                    in.close();
                        } catch (IOException ioe) {
                                    System.out.println(ioe.getMessage());
                        }
                        return prop;
            }

            /**
             * This method is responsible to load the property by reading from the file.
             *
             * @return String
             */
            public static String getPropertyValue(Properties prop, String propertyKey) {
                        String propertyValue = null;
                        boolean flag = prop.containsKey(propertyKey);
                        if (flag) {
                                    propertyValue = prop.getProperty(propertyKey);
                        }
                        return propertyValue;
            }

            @SuppressWarnings("rawtypes")
            public static void main(String args[]){
                        Properties prop = new Properties();
                        prop = PropertyFileLoader.loadPropertyFile("Test.properties");
                        Set propertySet = prop.entrySet();
                        System.out.println("AVAILABLE PROPERTIES ARE \n");
                        for( Object obj :propertySet){
                                    System.out.println(obj);
                        }
                        /** Checking the available properties in the property file*/
                        String propValue=PropertyFileLoader.getPropertyValue(prop, "ORG_NAME");
                        System.out.println("\nORG_NAME key property Value is-"+propValue);

                        /** Again Loading the property file using second method*/
                        PropertyFileLoader pfl = new PropertyFileLoader();
                        Properties prop1 = new Properties();
                        prop1 = pfl.loadPropertyFileUsingSecondWay("/com/gaurav/loadpropertyfile/employee.properties");
                        Set propertySet1 = prop1.entrySet();
                        System.out.println("\nAVAILABLE PROPERTIES USING SECOND METHOD ARE \n");
                        for( Object obj :propertySet1){
                                    System.out.println(obj);
                        }
                        /** Checking the available properties in the property file*/
                        String propValue1 =PropertyFileLoader.getPropertyValue(prop1, "COMP_NAME");
                        System.out.println("\nCOMP_NAME key property Value is-"+propValue1);
            }
}


Result:-


AVAILABLE PROPERTIES ARE

EMP_ID=1234
ORG_NAME=INDIA COMPANY
NAME=KUMAR GAURAV

ORG_NAME key property Value is-INDIA COMPANY

AVAILABLE PROPERTIES USING SECOND METHOD ARE

ID=9876
COMP_NAME=COMPANY LTD
EMP_NAME=EMPLOYEE 1

COMP_NAME key property Value is-COMPANY LTD


No comments:

Post a Comment