Sunday 28 July 2013

How to read the PLAIN property file and XML property file in java?



Below example is used to read the plain text property file as well as the XML format property file.

package com.gaurav.loadpropertyfile;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Set;

public class PropertyFileReader {

                public static void main(String[] args) throws IOException {
                                // This property is used for writing the File.
                                String plainPropertyFileName = "src/jdbc.properties";
                                String xmlPropertyFileName = "src/jdbc.xml";
                                /**
                                 * For reading the property from classpath no need to pass src
                                 * directory, as reading from classpath means automatically it will
                                 * search in the src directory and if file is not found then it will
                                 * throw Null Pointer Exception
                                 */
                                // This property is used for reading the File from classpath.
                                String propFileNameFromClasspath = "jdbc.properties";
                                getAllPropertyKeysFromFiles(plainPropertyFileName, xmlPropertyFileName);
                                getPropertiesFromFiles(plainPropertyFileName, xmlPropertyFileName);
                                getPropFileFromClasspath(propFileNameFromClasspath);
                }

                /**
                 * read property file from java classpath
                 *
                 * @param plainPropertyFileName
                 * @throws IOException
                 */
                private static void getPropFileFromClasspath(String plainPropertyFileName)
                                                throws IOException {
                                Properties prop = new Properties();
                                prop.load(PropertyFileReader.class.getClassLoader()
                                                                .getResourceAsStream(plainPropertyFileName));
                                System.out.println("Reading property file " + plainPropertyFileName
                                                                + " from the classpath");
                                System.out.println("jdbc.driverClassName = "
                                                                + prop.getProperty("jdbc.driverClassName"));
                                System.out.println("jdbc.url = " + prop.getProperty("jdbc.url"));
                                System.out.println("jdbc.username = "
                                                                + prop.getProperty("jdbc.username"));
                                System.out.println("jdbc.password = "
                                                                + prop.getProperty("jdbc.password"));
                                System.out
                                                                .println("Execution completed for reading property file "
                                                                                                + plainPropertyFileName + " from the classpath");
                                System.out
                                                                .println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");

                }

                /**
                 * read all the keys from the given property files
                 *
                 * @param plainPropertyFileName
                 * @param xmlPropertyFileName
                 * @throws IOException
                 */
                private static void getAllPropertyKeysFromFiles(
                                                String plainPropertyFileName, String xmlPropertyFileName)
                                                throws IOException {
                                System.out
                                                                .println("Execution started for getting all property Keys from files");
                                Properties prop = new Properties();
                                FileReader reader = new FileReader(plainPropertyFileName);
                                prop.load(reader);
                                Set<Object> keys = prop.keySet();
                                System.out.println("Reading keys from " + plainPropertyFileName
                                                                + " property file started");
                                for (Object obj : keys) {
                                                System.out.println("Key=" + obj.toString() + "||value="
                                                                                + prop.getProperty(obj.toString()));
                                }
                                // clearing existing properties and loading xml file
                                System.out.println("Reading keys from " + plainPropertyFileName
                                                                + " property file completed\n");
                                prop.clear();
                                InputStream is = new FileInputStream(xmlPropertyFileName);
                                prop.loadFromXML(is);
                                keys = prop.keySet();
                                System.out.println("Reading keys from " + xmlPropertyFileName
                                                                + " property file started");
                                for (Object obj : keys) {
                                                System.out.println("Key=" + obj.toString() + "||value="
                                                                                + prop.getProperty(obj.toString()));
                                }
                                System.out.println("Reading keys from " + xmlPropertyFileName
                                                                + " property file completed");
                                // closing and closing all the resources
                                is.close();
                                reader.close();
                                System.out
                                                                .println("Execution completed for getting all property Keys from files\n");
                                System.out
                                                                .println("******************************************************************\n");
                }

                /**
                 * This method reads plain property as well as XML property files from file
                 * system
                 *
                 * @param plainPropertyFileName
                 * @param xmlPropertyFileName
                 * @throws IOException
                 * @throws FileNotFoundException
                 */
                private static void getPropertiesFromFiles(String plainPropertyFileName,
                                                String xmlPropertyFileName) throws FileNotFoundException,
                                                IOException {
                                System.out.println("Start of getting properties from files");
                                Properties prop = new Properties();
                                FileReader reader = new FileReader(plainPropertyFileName);
                                prop.load(reader);
                                System.out.println("Reading " + plainPropertyFileName
                                                                + " property file started");
                                System.out.println("jdbc.driverClassName = "
                                                                + prop.getProperty("jdbc.driverClassName"));
                                System.out.println("jdbc.url = " + prop.getProperty("jdbc.url"));
                                System.out.println("jdbc.username = "
                                                                + prop.getProperty("jdbc.username"));
                                System.out.println("jdbc.password = "
                                                                + prop.getProperty("jdbc.password"));
                                System.out.println("Reading " + plainPropertyFileName
                                                                + " property file completed\n");
                                // loading xml file now, first clear existing properties
                                prop.clear();
                                InputStream is = new FileInputStream(xmlPropertyFileName);
                                prop.loadFromXML(is);
                                System.out.println("Reading " + xmlPropertyFileName
                                                                + " property file started");
                                System.out.println("jdbc.driverClassName = "
                                                                + prop.getProperty("jdbc.driverClassName"));
                                System.out.println("jdbc.url = " + prop.getProperty("jdbc.url"));
                                System.out.println("jdbc.username = "
                                                                + prop.getProperty("jdbc.username"));
                                System.out.println("jdbc.password = "
                                                                + prop.getProperty("jdbc.password"));
                                System.out.println("Reading " + xmlPropertyFileName
                                                                + " property file completed");
                                // closing and Releasing all the resources
                                is.close();
                                reader.close();
                                System.out.println("End of getting properties from files");
                                System.out
                                                                .println("####################################################################\n");
                }

}

Result:-

Execution started for getting all property Keys from files
Reading keys from src/jdbc.properties property file started
Key=jdbc.username||value=root
Key=jdbc.password||value=root
Key=jdbc.driverClassName||value=com.mysql.jdbc.Driver
Reading keys from src/jdbc.properties property file completed

Reading keys from src/jdbc.xml property file started
Key=jdbc.username||value=system
Key=jdbc.password||value=system123
Key=jdbc.driverClassName||value=oracle.jdbc.driver.OracleDriver
Reading keys from src/jdbc.xml property file completed
Execution completed for getting all property Keys from files

******************************************************************

Start of getting properties from files
Reading src/jdbc.properties property file started
jdbc.driverClassName = com.mysql.jdbc.Driver
jdbc.url = null
jdbc.username = root
jdbc.password = root
Reading src/jdbc.properties property file completed

Reading src/jdbc.xml property file started
jdbc.driverClassName = oracle.jdbc.driver.OracleDriver
jdbc.url = null
jdbc.username = system
jdbc.password = system123
Reading src/jdbc.xml property file completed
End of getting properties from files
####################################################################

Reading property file jdbc.properties from the classpath
jdbc.driverClassName = com.mysql.jdbc.Driver
jdbc.url = null
jdbc.username = root
jdbc.password = root
Execution completed for reading property file jdbc.properties from the classpath
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


No comments:

Post a Comment