Most of the time we created the properties file and modifying it in text editors like Notepad++ and notpad etc. We also can create and modifies the properties files from Java program as well. You will see in the following example XmlTextPropsReaderWriter.java how to do it.
The most common properties files are Log4j.properties that is used to configure Log4J system logging, and jdbc.properties which also used to control your program JDBC API connectivity parameters, and finally no web application working with internationalization (I18N) doesn’t have a resource bundle in form of properties file.
Let's see how to read and create properties file from java program in both formats (XML and TEXT).
Java API’s java.util.Properties class provides 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 property file in XML format. On the other hand the load() reads a text format properties file while loadFromXML() reads XML format properties file.
The following example is streamlined using new JDK7 feature and NIO.2 capabilities.
Program source:
Running the above program produces the following output:
value1
value3
The created properties file text (users.properties) format is:
And the created properties file XML (user.xml) format is:
Note that: xml file created is governed by the following properties.dtd specification file:
Although most of us even me by default using the normal text format of the properties file and statically defined. Then the questions that may raise here are:
1- When to use the XML format?
2- When we can benefit from creating it dynamically?
for the first question, there are many areas that you can benefit from the xml format especially when you working with the same configuration file from many systems, and working with different languages to specific devices and need one configuration understood by all languages, readable, easy to secure, and retrieving data using xPath API is doable, all of these benefits leads to xml properties format.
The second part is, whenever you want to create a dynamic configuration, then you have the solution now.
The most common properties files are Log4j.properties that is used to configure Log4J system logging, and jdbc.properties which also used to control your program JDBC API connectivity parameters, and finally no web application working with internationalization (I18N) doesn’t have a resource bundle in form of properties file.
Let's see how to read and create properties file from java program in both formats (XML and TEXT).
Java API’s java.util.Properties class provides 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 property file in XML format. On the other hand the load() reads a text format properties file while loadFromXML() reads XML format properties file.
The following example is streamlined using new JDK7 feature and NIO.2 capabilities.
Program source:
Running the above program produces the following output:
value1
value3
The created properties file text (users.properties) format is:
And the created properties file XML (user.xml) format is:
Note that: xml file created is governed by the following properties.dtd specification file:
Although most of us even me by default using the normal text format of the properties file and statically defined. Then the questions that may raise here are:
1- When to use the XML format?
2- When we can benefit from creating it dynamically?
for the first question, there are many areas that you can benefit from the xml format especially when you working with the same configuration file from many systems, and working with different languages to specific devices and need one configuration understood by all languages, readable, easy to secure, and retrieving data using xPath API is doable, all of these benefits leads to xml properties format.
The second part is, whenever you want to create a dynamic configuration, then you have the solution now.