What is a Properties File in Java?
In Java, a properties file is a simple text-based file used to store configuration or settings in key-value pairs. It provides a convenient way to externalize application-specific parameters that can be easily modified without modifying the source code.
Properties File always stores information about the configuration parameters such as project configuration data such as URL, username, password, and project settings config like browser name, environment, and so on.
Properties files have a “.properties” extension.
How to write data in a Properties File in XML Format?
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.
You can write properties to a properties file using the Properties class. Create a Properties object and set the key-value pairs
Properties properties = new Properties();
properties.setProperty("username", "Vibha");
properties.setProperty("password", "XML");
Specify the output file path and create an FileOutputStream object.
FileOutputStream fileOutputStream = new FileOutputStream("src/test/resources/userCreatedXML.xml");
Write the properties to the output stream in XML format
properties.storeToXML(fileOutputStream, "Sample XML Properties file created");
Below is a complete example:
package org.example;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class WritePropertiesXMLExample {
public static void main(String args[]) {
Properties properties = new Properties();
properties.setProperty("username", "Vibha");
properties.setProperty("password", "XML");
try {
// userCreated.properties is created at the mentioned path
FileOutputStream fileOutputStream = new FileOutputStream("src/test/resources/userCreatedXML.xml");
// storeToXML() method is used to write the properties into properties xml file
properties.storeToXML(fileOutputStream, "Sample XML Properties file created");
fileOutputStream.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
FileOutputStream: This class is used to create or overwrite the existing property file.
Pass this stream into the “storeToXML()” method of “Properties”, which writes the contents of the “Properties” object into the file in XML Format.
The output of the above execution is
userCreatedXML.xml file is created in src/test/resources.

The userCreatedXML.xml file is shown below:

Thank you for completing this tutorial! I’m glad you found it helpful. Happy learning and cheers to your success!