Java Convert Properties File to XML File

Java example to create XML file from Properties object or any existing .properties file. To convert an XML file to .properties file, follow the steps given in linked tutorial.

1. Create XML File from Properties File

To convert the properties file into an XML file, the best way is to use java.util.Properties class.

The process is :

  1. Load properties file into class object.
  2. Use Properties.storeToXML() method to write the content as XML.
String inPropertiesFile = "";
String outXmlFile = "applicationProperties.xml";

InputStream is = new FileInputStream(inPropertiesFile); //Input file
OutputStream os = new FileOutputStream(outXmlFile);   //Output file

Properties props = new Properties();

//properties to XML conversion
props.storeToXML(os, "","UTF-8");

2. Demo

The Input Properties File is as follows which we will convert to XML.

#Disable batch job's auto start

#batch input files location

The output XML file after the conversion is as follows:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "">
  <entry key="input.dir">c:/temp/input</entry>
  <entry key="spring.batch.job.enabled">false</entry>
  <entry key="spring.main.banner-mode">off</entry>

Drop me your questions in the comment section.

Happy Learning !!


Notify of
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.