Sunday, 28 July 2013

How to create a property file using Java program in Plain Text and XML format?



Creating a property file using Java program in Plain Text and XML format


java.util.Properties package of Java API provides multiple utility store() methods to store properties in text or xml format.  The store() method can be used to create a plain text property file where as  storeToXML() method can be used for creating a Java property file in XML format.


Below are the examples used for the creation of plain text property file and also for the creation of XML property file.


package com.gaurav.loadpropertyfile;

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

public class PropertyFileWriter {
                /**
                 * This method will populate or write properties in a plain property file
                 *
                 * @param plainPropertyFileName
                 * @throws IOException
                 */
                private static void createPlainPropertyFile(String plainPropertyFileName)
                                                throws IOException {
                                System.out.println("Start Writing the PLAIN property file ");
                                Properties prop = new Properties();
                                prop.setProperty("jdbc.driverClassName", "com.mysql.jdbc.Driver");
                                prop.setProperty("jdbc.username", "root");
                                prop.setProperty("jdbc.password", "root");
                                prop.store(new FileWriter(plainPropertyFileName),
                                                                "JDBC Connection Config file");
                                System.out.println(plainPropertyFileName + " written successfully");
                                System.out.println("End of writing the PLAIN property file");

                }

                /**
                 * This method will populate or write properties in a XML property file
                 *
                 * @param xmlPropertyFileName
                 * @throws IOException
                 */
                private static void createXMLPropertyFile(String xmlPropertyFileName)
                                                throws IOException {

                                System.out.println("Start writing the XML property file");
                                Properties prop = new Properties();
                                prop.setProperty("jdbc.driverClassName",
                                                                "oracle.jdbc.driver.OracleDriver");
                                prop.setProperty("jdbc.username", "system");
                                prop.setProperty("jdbc.password", "system123");
                                prop.storeToXML(new FileOutputStream(xmlPropertyFileName),
                                                                "JDBC Connection Config XML file");
                                System.out.println(xmlPropertyFileName + " written successfully");
                                System.out.println("End of writing the XML Property File");
                }

                public static void main(String[] args) throws IOException {
                                // Below two properties are the name of the created property files
                                String plainPropertyFileName = "src/jdbc.properties";
                                String xmlPropertyFileName = "src/jdbc.xml";
                                createPlainPropertyFile(plainPropertyFileName);
                                createXMLPropertyFile(xmlPropertyFileName);
                }
}

Result:-

Start Writing the PLAIN property file
src/jdbc.properties written successfully
End of writing the PLAIN property file
Start writing the XML property file
src/jdbc.xml written successfully
End of writing the XML Property File

Created jdbc.property file content:-

#JDBC Connection Config file
#Sun Jul 28 13:31:56 IST 2013
jdbc.username=root
jdbc.password=root
jdbc.driverClassName=com.mysql.jdbc.Driver

Created jdbc.xml file content:-

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>JDBC Connection Config XML file</comment>
<entry key="jdbc.username">system</entry>
<entry key="jdbc.password">system123</entry>
<entry key="jdbc.driverClassName">oracle.jdbc.driver.OracleDriver</entry>
</properties>



No comments:

Post a Comment