Java Properties: Storing and Writing Data in XML Format

HOME

Properties properties = new Properties();
properties.setProperty("username", "Vibha");
properties.setProperty("password", "XML");
 FileOutputStream fileOutputStream = new FileOutputStream("src/test/resources/userCreatedXML.xml");

properties.storeToXML(fileOutputStream, "Sample XML Properties file created");
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();
        }
    }
}

Leave a comment