Tuesday, May 16, 2023

How to Create and Modify Properties File Form Java Program in Text and XML Format - Example

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.

How to Create and Modify Properties File Form Java Program in Text and XML Format - Example



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 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 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 properties 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 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

2 comments:

  1. Merry Christmas. And nice tutorials, indeed.

    I had a real situation for using this very tutorial, if you want to know: an old application that had all the configuration in a database table, that came with bad performance and poor reliability.

    So, we wrote a class to read from the table to a properties file. Usefull, and a great improvement.

    By the way, the program was written in Windows, but the production platform is AIX (IBM Unix), so it's better to manage filesystem names in an agnostic way, I mean, no "c:\".

    The only problem we had with Java path is to know where is the user.dir, and to configure appropriately this value in the unit testing. It's not easy when you have to change between Java SE (unit tests) and Java EE (testing in local server), but at the end, we did it.

    ReplyDelete
  2. Javin, if you need an online solution to manage XML localization, check out the software localization tool POEditor. It's good for translating strings in a team and it also supports Java properties files.

    ReplyDelete