How to Create and Modify Properties File Form Java Program in Text and XML Format - Example (original) (raw)
Though most of the time we create and modify properties file using text editors 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
As 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 the File system. Here is a 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 formatand 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 classTextPropertyWriter {
public static voidmain(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 classXmlPropertiesWriter {
public static voidmain(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 properties into properties file from Java
props.storeToXML(fos, "Properties file in xml format generated from Java program");
fos.close();
}
}
Output:
Properties file in xml format generated from Java program
<entry** key="key2"**>value2
<entry** key="key1"**>value1
That’s all on How to create a Properties file from the Java program or How to modify Properties from the Java program. As I said almost all the time we use text editor e.g. notepad or notepad++ to edit properties files like log4j.properties or jdbc.properties, you can also edit them from the Java program if needed.
I personally prefer properties file in text format if the number of properties is not much and XML-based properties file if the file is big enough like if you have many properties to leverage XML editors.
Other Java XML tutorials you may find useful