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
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read