How to Create and Modify Properties File Form Java Program in Text and XML Format?
Properties file are a text-oriented key value a pair of contents present in a Java project with .properties extension. Line by line, the key-value pair of contents are present, and they are usually prepared by means of notepad, Wordpad, EditPlus, etc., Properties files are usually helpful to store important sensitive information and in this article, let us see how to create properties file using Java programs.
Java API got java.util.Properties class and it has several utility stores() methods to store properties in either Text or XML format. In order to store property in text format, Store() method can be used. storeToXML() is used to make in XML format.
store() method took two parameters like Output Stream and Comments.
Text format creation:
Let us see how to create a properties file in a text format. As we are creating properties contents, a valid file path location needs to be given.
We are using store() to save the properties file in text format. As key-value pairs, they are set. i.e. key = value. Other than that, all are considered as comments and hence with # symbol, comments are placed.
Usage of properties in text format is helpful in many instances when the property contents are less, and the team of developers is often changing, and end-users are on the Non-IT side.
XML format creation:
In many instances, XML is needed which provides an easy-to-understand and efficient format for storing important sensitive information. Extensible Markup Language (XML) is a markup language, and it is having a set of rules for encoding documents, and they are understandable in both human-readable and machine-readable format. Here, let us see how to create via Java program
If we check out the output of XML, it has an equal opening and closing of entries.
The one created via java programs also has the same structure. It has started to have with <properties> and end with </properties>. Other than key-value pair sets, the text is treated as comments and hence they are inside the comment tags. Properties files are having key values only and here also it is declared within “entry” tags along with “key=” which means separate entries are given for each and every key-value pair.
Whenever the properties file contents are huge and having sensitive information like banking transactions, financial data, etc., it is better to go in XML format only.
A convenient way of converting XML contents to Read-only Text Mode way
Input file (sampleapplicationProperties.xml)
Generated Output file(sampleapplication.properties)
And also as we have, System.out.println(properties.get(“username”)); , it displays
“Geek” as output. So loadFromXML() helps to load the XML file and converts that to text-oriented properties file by means of the store() and also as got converted, we can get the property values easily.
In this article, we have seen the ways of creation of properties files from java programs. Enjoy them to your convenience. They are helpful in any part of the software project as properties files are key files that hold sensitive information and also as they are in key-value pairs either as text or XML format, dynamic usage is seen, and at any point of time, we can modify them easily too.
Please Login to comment...