Program to Convert Property file to Xml File in java

We will read from c:/MyPropertyFile.properties and convert it to c:/MyXmlFile.xml


c:/MyPropertyFile.properties looks like this >
#This is MyPropertyFile
#Wed Aug 26 18:32:05 IST 2015
Language=Java
myName=Ankit



Program to Convert Property file to Xml File in java >

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
/** JavaMadeSoEasy.com */
public class ConvertPropertyToXmlFile {
public static void main(String[] args) throws IOException {
     String propFileName = "c:/MyPropertyFile.properties";
     // 1) Create FileInputStream
     FileInputStream fis = new FileInputStream(propFileName);
     // 2) Create java.util.Properties and load from FileInputStream
     Properties prop = new Properties();
     prop.load(fis);
    
     String xmlFileName = "c:/MyXmlFile.xml";
     // 3) Create Xml file
     FileOutputStream fos = new FileOutputStream(xmlFileName);
     prop.storeToXML(fos, "Dynamic Property File");
     System.out.println(xmlFileName + " has been created from "
              + propFileName);
}
}
/*OUTPUT
c:/MyXmlFile.xml has been created from c:/MyPropertyFile.properties
*/


c:/MyXmlFile.xml will look like this >
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Dynamic Property File</comment>
<entry key="Language">Java</entry>
<entry key="myName">Ankit</entry>
</properties>

For more convenience below I have attached screenshot as well.

No comments:

Post a Comment