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.Propertiespackage 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.


packagecom.gaurav.loadpropertyfile;

importjava.io.FileOutputStream;
importjava.io.FileWriter;
importjava.io.IOException;
importjava.util.Properties;

public classPropertyFileWriter {
                /**
                 * This method will populate or write properties in a plain property file
                 *
                 * @param plainPropertyFileName
                 * @throws IOException
                 */
                private static voidcreatePlainPropertyFile(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 voidcreateXMLPropertyFile(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 Configfile
#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 propertiesSYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>JDBC Connection ConfigXML 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