Properties storeToXML(OutputStream, String) in Java

storeToXML(OutputStream, String): This method is available in java.util.Properties class of Java.

Syntax:

void java.util.Properties.storeToXML(OutputStream os, String comment) throws IOException

This method takes two arguments. This method emits an XML document representing all of the properties contained in this table.

Parameters: Two parameters are required for this method.

os: the output stream on which to emit the XML document.

comment: a description of the property list, or null if no comment is desired.

Throws:

1. IOException - if writing to the specified output stream results in an IOException.

2. NullPointerException - if the os is null.

3. ClassCastException - if this Properties object contains any keys or values that are not Strings.

Approach 1: When no exception

Java

import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;

public class PropertiesstoreToXML {
    public static void main(String[] args) throws IOException {

        Properties properties = new Properties();

        properties.put("Hello", "World");
        properties.put("C++", "Program");
        properties.put("Java", "Program");

        OutputStream os = System.out;
        properties.storeToXML(os, null);

    }
}

Output:

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties> <entry key="Java">Program</entry> <entry key="C++">Program</entry> <entry key="Hello">World</entry> </properties>



Approach 2: NullPointerException

Java

import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;

public class PropertiesstoreToXML {
    public static void main(String[] args) throws IOException {

        Properties properties = new Properties();

        properties.put("Hello", "World");
        properties.put("C++", "Program");
        properties.put("Java", "Program");

        OutputStream os = null;
        properties.storeToXML(os, null);

    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: OutputStream at java.base/java.util.Objects.requireNonNull(Objects.java:233) at java.base/java.util.Properties.storeToXML(Properties.java:1104) at java.base/java.util.Properties.storeToXML(Properties.java:1007)


No comments:

Post a Comment