How to Create and Modify Properties File Form Java Program in Text and XML Format?
Last Updated :
21 Dec, 2021
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.
Java
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class CreationOfTextOrientedProperties {
public static void main(String args[])
throws FileNotFoundException, IOException
{
// Creating properties files from Java program
Properties properties = new Properties();
// In the name of userCreated.properties, in the
// current directory location, the file is created
FileOutputStream fileOutputStream
= new FileOutputStream(
"userCreated.properties");
// As an example, given steps how
// to keep username and password
properties.setProperty("username", "value1");
properties.setProperty("password", "value2");
// writing properties into properties file
// from Java As we are writing text format,
// store() method is used
properties.store(
fileOutputStream,
"Sample way of creating Properties file from Java program");
fileOutputStream.close();
}
}
Output:

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
Java
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class CreationOfXMLOrientedProperties {
public static void main(String args[])
throws FileNotFoundException, IOException
{
// Creating properties files from Java program
Properties properties = new Properties();
// In the name of userCreated.xml, in the current
// directory location, the file is created
FileOutputStream fileOutputStream
= new FileOutputStream("userCreated.xml");
// As an example, given steps how to keep username
// and password
properties.setProperty("username", "value1");
properties.setProperty("password", "value2");
// writing properties into properties file
// from Java As we are writing in XML format,
// storeToXML() method is used
properties.storeToXML(
fileOutputStream,
"Sample way of creating Properties file from Java program");
fileOutputStream.close();
}
}
Output:

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
Java
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;
public class ConvertXMLToTextOrientedProperties {
public static void main(String[] args)
throws InvalidPropertiesFormatException, IOException
{
String outputPropertiesFile
= "sampleapplication.properties";
String inputXmlFile
= "sampleapplicationProperties.xml";
// Input XML File which contains
// necessary information
InputStream inputStream
= new FileInputStream(inputXmlFile);
// Output properties File
OutputStream outputStream
= new FileOutputStream(outputPropertiesFile);
Properties properties = new Properties();
// Load XML file that has necessary information
properties.loadFromXML(inputStream);
// Store to properties file via this way
properties.store(
outputStream,
"Converted from sampleapplicationProperties.xml");
// For sample testing let us get username--It is
// nothing but "Geek"
// As it is converted to .properties file,
// we can get the values in this way
System.out.println(properties.get("username"));
}
}
Input file (sampleapplicationProperties.xml)
XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Elegant way of converting sampleapplicationProperties.xml
to Sampleapplication.properties</comment>
<entry key="username">Geek</entry>
<entry key="password">XXXXWeldoneXXXX</entry>
</properties>
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.
Conclusion :
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.
Similar Reads
How to Create a .exe File From a Java Program?
In this article, we will learn how to create a .exe file from a Java program. Java is a platform-independent language, which works on the philosophy of "write Once, Run Anywhere". It simply means that if we write a Java program, we can run it on any device that has a JVM(Java Virtual Machine). Now i
5 min read
Java program to store a Student Information in a File using AWT
Swing is a part of the JFC (Java Foundation Classes). Building Graphical User Interface in Java requires the use of Swings. Swing Framework contains a large set of components which allow a high level of customization and provide rich functionalities, and is used to create window-based applications.
4 min read
How to Create Different Data Format of Cells in a Spreadsheet Using Java?
Formatting in excel is a neat trick in excel which is used to change the appearance of the data represented in the worksheet, formatting can be done in multiple ways such as we can format the data of the cells by using the styles. By default, all worksheet cells are formatted with the General number
4 min read
How to Write Data from Excel File into a HashMap using Java and Apache POI?
Apache POI is an open-source java library to create and manipulate various file formats based on Microsoft Office. Using POI, one should be able to perform create, modify and display/read operations on the following file formats. For Example, Java doesnât provide built-in support for working with ex
3 min read
C# Program to Check the Information of the File
Given a file, now our task is to view the information of this file through C#. So to do this task we use FileInfo class. This class provides different types of properties and methods for creating, copying, deleting, opening, and moving files. It is also, used for creating FileStream objects. So here
1 min read
How to call testng.xml file from pom.xml in Maven?
Maven is a tool that is widely used for automation whenever developers work on Java projects. apart from the Java language, the Maven software supports other projects that are written in C#, Ruby, etc. The Maven project is controlled and hosted by the famous Apache software foundation.Table of Conte
4 min read
How to Read Data From Properties File in Selenium?
Selenium is the most popular open source web automation tool, It enables the user to write code in Java, Python, JavaScript, and many other programming languages.Why Properties File?While automating a web application many user operations have to be performed and much input data is required to perfor
3 min read
How to Create Jar File for Java Project in Eclipse?
Java programmers already know about what is a jar file if you are new then first understand what is a jar file. Once you are completed your project in Eclipse the next task is to create a runnable version of your project. Eclipse support only exporting the JAR (.jar) file, not the Executable (.exe)
3 min read
Java Program to Append a String in an Existing File
In Java, we can append a string in an existing file using FileWriter which has an option to open a file in append mode. Java FileWriter class is used to write character-oriented data to a file. It is a character-oriented class that is used for file handling in Java. Unlike FileOutputStream class, we
3 min read
Spring - How to Load Literal Values From Properties File
Literals in Java are a synthetic representation of boolean, numeric, character, or string data. It is a medium of expressing particular values in the program, such as an integer variable named ââ/count is assigned an integer value in the following statement. int x = 100; // Here 100 is a constant/li
4 min read