Sunday, 26 January 2014

How to create and modify Properties file form Java program in Text and XML format

Though most of the time we create and modify properties file using text editor like notepad, word-pad or edit-plus, It’s also possible to create and edit properties file from Java program. Log4j.properties, which is used to configure Log4J based logging in Java and jdbc.properties which is used to specify configuration parameters for database connectivity using JDBC are two most common example of property file in Java. Though I have not found any real situation where I need to create properties file using Java program but it’s always good to know about facilities available in Java API. In last Java tutorial on Properties we have seen how to read values from properties file on both text and XML format and in this article we will see how to create properties file on both text and XML format. Java API’s java.util.Properties class provides several utility store() methods to store properties in either text or xml format. Store() can be used to store property in text properties file and  storeToXML() method can be used for creating a Java property file in XML format.

Java program to create and store Properties in text and XML format

How to create Property file from Java program text and XML formatAs I said earlier java.util.Properties represent property file in Java program. It provides load() and store() method to read and write properties files from and to File system. Here is simple example of creating Java property file form program itself.

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

/**
 * Java program to create and modify property file in text format and storing
 * property on it. Most of the time we use text editor to create and edit property
 * file e.g. jdbc.properties or log4j.properties but we can also create property
 * file from Java program as shown in this example.
 *
 * @author Javin Paul
 */

public class TextPropertyWriter {

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

        //Creating properties files from Java program
        Properties props = new Properties();
        FileOutputStream fos = new FileOutputStream("c:/user.properties");
     
        props.setProperty("key1", "value1");
        props.setProperty("key2", "value2");
     
        //writing properites into properties file from Java
        props.store(fos, "Properties file generated from Java program");
     
        fos.close();
     
    }
}

This will create user.properties file in C:\. here is how that property file will look like:

#Properties file generated from Java program
#Mon Jan 16 03:00:57 VET 2012
key2=value2
key1=value1

Here is another example of creating Java property file in XML format from Java program:

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

/**
 * Java program to store properties in XML property file. stroeToXML() method of
 * java.util.Properties class is used to save properties in XML property file from Java
 * program.
 *
 * @author Javin Paul
 */

public class XmlPropertiesWriter {

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

        //Reading properties files in Java example
        Properties props = new Properties();
        FileOutputStream fos = new FileOutputStream("c:/user.xml");
     
        props.setProperty("key1", "value1");
        props.setProperty("key2", "value2");
     
        //writing properites into properties file from Java
        props.storeToXML(fos, "Properties file in xml format generated from Java program");
     
        fos.close();

     
    }
}
Output:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Properties file in xml format generated from Java program</comment>
<entry key="key2">value2</entry>
<entry key="key1">value1</entry>
</properties>

That’s all on How to create Properties file from Java program or How to modify Properties from Java program. As I said almost all time we use text editor e.g. notepad or notepad++ to edit properties file like log4j.properties or jdbc.properties, you can also edit them from Java program if needed. I personally prefer properties file in text format if number of properties is not much and XML based properties file if file is big enough e.g. if you have many properties to leverage XML editors.

Other Java XML tutorials you may find useful

No comments:

Post a Comment