storeToXML(OutputStream, String, Charset): This method is available in java.util.Properties class of Java.
Syntax:
void java.util.Properties.storeToXML(OutputStream os, String comment, Charset charset) throws IOException
This method takes three arguments. This method emits an XML document representing all of the properties contained in this table, using the specified encoding.
Parameters: Three 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.
charset: the charset.
Throws:
1. IOException - if writing to the specified output stream results in an IOException.
2. NullPointerException - if os or charset 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.nio.charset.Charset;import java.util.Properties;public class PropertiesstoreToXML2 {public static void main(String[] args) throws IOException {Properties properties = new Properties();properties.put("Hello", "World");properties.put("C++", "Program");properties.put("Java", "Program");properties.storeToXML(System.out, null,Charset.defaultCharset());}}
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.nio.charset.Charset;import java.util.Properties;public class PropertiesstoreToXML2 {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,Charset.defaultCharset());}}
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)
No comments:
Post a Comment