Sunday, January 8, 2017

PropertyUtility class for reading properties using Java


In this post, we will see how to get the properties from multiple property file.

There are 3 ways to get the properties from property file:
  1. Using Class path
  2. Using Relative path
  3. Using Absolute path
1. Using Class Path: In order to get properties from class path, we need to use getResourceAsStream().
Example: 
InputStream inputStream = PropertyUtility.class.getResourceAsStream(propertyFileName);
properties.load(inputStream);
inputStream.close();
2. Using Relative & Absolute Path: In order to get properties from relative/absolute path we need to use FileInputStream()
File file = new File(propertyFileName);
InputStream inputStream = = new FileInputStream(file);
properties.load(inputStream);
inputStream.close();
Program:
package com.ranga.utility;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Utility class for getting the properties
 * 
 * @author Ranga Reddy
 * @since 01-Jan-2017
 * @version 1.0
 */
public class PropertyUtility {

    /**
     * Getting the properties from multiple property files.
     * 
     * @param propertyFileNames
     *            - property file names
     * @return - the properties
     * @throws IOException
     */
    public static Properties getProperties(String... propertyFileNames) throws IOException {
        Properties properties = null;
        if (propertyFileNames != null) {
            properties = new Properties();
            for (String propertyFileName : propertyFileNames) {
                File file = new File(propertyFileName);
                InputStream inputStream = null;
                if (file.exists()) {
                    System.out.println("File <" + propertyFileName + "> found in the FileSystem.");
                    inputStream = new FileInputStream(file);
                } else {
                    inputStream = PropertyUtility.class.getResourceAsStream(propertyFileName);
                    if (inputStream == null) {
                        throw new FileNotFoundException("File <" + propertyFileName + "> is not found.");
                    }
                    System.out.println("File <" + propertyFileName + "> found in the classpath.");
                }
                properties.load(inputStream);
                inputStream.close();
            }
        }
        return properties;
    }
}
Testing the Above code:
package com.ranga.utility;

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

/**
 * Test Utility class for testing the properties
 * @author Ranga Reddy
 * @since 01-Jan-2017
 * @version 1.0
 */
public class PropertyUtilityTest {

    public static void main(String[] args) throws IOException {

        // loading the properties from class path file
        Properties classPathProperties = PropertyUtility.getProperties("/test.properties");
        System.out.println("Class Path Properties: " + classPathProperties + "\n");

        // loading the properties from relative path.
        Properties relativePathProperties = PropertyUtility.getProperties("test2.properties");
        System.out.println("Relative Path Properties: " + relativePathProperties + "\n");

        // loading the properties from absolute file path.
        Properties absolutePathProperties = PropertyUtility.getProperties(
                "C:\\Users\\RangaReddy\\Ranga_Utilities\\test2.properties");
        System.out.println("Absolute Path Properties: " + absolutePathProperties + "\n");

        // loading the properties from multiple files
        Properties multipleFileProperties = PropertyUtility.getProperties(
                "/test.properties", "test2.properties");
        System.out.println("Multiple file Properties: " + multipleFileProperties + "\n");

        // loading the properties from unknown file path.
        Properties unknownPathProperties = PropertyUtility.getProperties("unknown.properties");
        System.out.println("Unknown Path Properties: " + unknownPathProperties + "\n");
    }
}
Output:
File </test.properties> found in the classpath.
Class Path Properties: {tp2=value2, tp1=value1}

File <test2.properties> found in the FileSystem.
Relative Path Properties: {t2p1=value1, t2p2=value2}

File <C:\Users\RangaReddy\Ranga_Utilities\test2.properties> found in the FileSystem.
Absolute Path Properties: {t2p1=value1, t2p2=value2}

File </test.properties> found in the classpath.
File <test2.properties> found in the FileSystem.
Multiple file Properties: {t2p1=value1, tp2=value2, tp1=value1, t2p2=value2}

Exception in thread "main" java.io.FileNotFoundException: File <unknown.properties> is not found.
    at com.ranga.utility.PropertyUtility.getProperties(PropertyUtility.java:38)
    at com.ranga.utility.PropertyUtilityTest.main(PropertyUtilityTest.java:38)
Happy Learning ...!

0 comments: