Java Properties storeToXML(OutputStream os, String comment) Method



Description

The Java Properties storeToXML(OutputStream os, String comment) method emits an XML document representing all of the properties contained in this table.An invocation of this method of the form props.storeToXML(os, comment) behaves in exactly the same way as the invocation props.storeToXML(os, comment, "UTF-8");

Declaration

Following is the declaration for Java Properties storeToXML() method

public void storeToXML(OutputStream os,String comment)

Parameters

  • out − the output stream on which to emit the XML document.

  • comments − a description of the property list, or null if no comment is desired.

Return Value

This method does not return a value

Exception

  • IOException − if writing this property list to the specified output stream throws an IOException.

  • ClassCastException − if this Properties object contains any keys or values that are not Strings.

  • NullPointerException − if out is null.

Java Properties storeToXML(OutputStream os, String comment,String encoding) Method

Description

The java.util.Properties.storeToXML(OutputStream osString comment,String encoding) method emits an XML document representing all of the properties contained in this table, using the specified encoding.

Declaration

Following is the declaration for java.util.Properties.storeToXML() method

public void storeToXML(OutputStream os,String comment, String encoding)

Parameters

  • out − the output stream on which to emit the XML document.

  • comments − a description of the property list, or null if no comment is desired.

  • encoding − the name of a supported character encoding.

Return Value

This method does not return a value

Exception

  • IOException − if writing this property list to the specified output stream throws an IOException.

  • ClassCastException − if this Properties object contains any keys or values that are not Strings.

  • NullPointerException − if os or encoding is null.

  • UnsupportedEncodingException − if the encoding is not supported by the implementation.

Java Properties storeToXML(OutputStream os, String comment,Charset encoding) Method

Description

The java.util.Properties.storeToXML(OutputStream osString comment,Charset encoding) method emits an XML document representing all of the properties contained in this table, using the specified encoding.

Declaration

Following is the declaration for java.util.Properties.storeToXML() method

public void storeToXML(OutputStream os,String comment, Charset encoding)

Parameters

  • out − the output stream on which to emit the XML document.

  • comments − a description of the property list, or null if no comment is desired.

  • charset − the charset.

Return Value

This method does not return a value

Exception

  • IOException − if writing this property list to the specified output stream throws an IOException.

  • ClassCastException − if this Properties object contains any keys or values that are not Strings.

  • NullPointerException − if os or charset is null.

Storing Entries of Properties to an XML File Example

The following example shows the usage of Java Properties storeToXML(OutputStream os, String comment) method to store the properties object into xml file and then xml is printed.

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class PropertiesDemo {
   public static void main(String[] args) {
      Properties prop = new Properties();

      // add some properties
      prop.put("Height", "200");
      prop.put("Width", "15");

      try {

         // create a output and input as a xml file
         FileOutputStream fos = new FileOutputStream("properties.xml");
         FileInputStream fis = new FileInputStream("properties.xml");

         // store the properties in the specific xml
         prop.storeToXML(fos, "Properties Example");

         // print the xml
         while (fis.available() > 0) {
            System.out.print("" + (char) fis.read());
         }
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Properties Example</comment>
<entry key="Height">200</entry>
<entry key="Width">15</entry>
</properties>

Storing Entries of Properties to an XML File Using Given Encoding Example

The following example shows the usage of Java Properties storeToXML(OutputStream os, String comment, String encoding) method to store the properties object into xml file using given encoding and then xml is printed.

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class PropertiesDemo {
   public static void main(String[] args) {
      Properties prop = new Properties();

      // add some properties
      prop.put("Height", "200");
      prop.put("Width", "15");

      try {

         // create a output and input as a xml file
         FileOutputStream fos = new FileOutputStream("properties.xml");
         FileInputStream fis = new FileInputStream("properties.xml");

         // store the properties in the specific xml and a different encoding
         prop.storeToXML(fos, "Properties Example","windows-1252");

         // print the xml. Notice that ISO 8859 isn't supported
         while (fis.available() > 0) {
            System.out.print("" + (char) fis.read());
         }
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

<?xml version="1.0" encoding="windows-1252"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Properties Example</comment>
<entry key="Height">200</entry>
<entry key="Width">15</entry>
</properties>

Storing Entries of Properties to an XML File Using Default Encoding Example

The following example shows the usage of Java Properties storeToXML(OutputStream os, String comment, Charset encoding) method to store the properties object into xml file using given encoding and then xml is printed.

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Properties;

public class PropertiesDemo {
   public static void main(String[] args) {
      Properties prop = new Properties();

      // add some properties
      prop.put("Height", "200");
      prop.put("Width", "15");

      try {

         // create a output and input as a xml file
         FileOutputStream fos = new FileOutputStream("properties.xml");
         FileInputStream fis = new FileInputStream("properties.xml");

         // store the properties in the specific xml and a different encoding
         prop.storeToXML(fos, "Properties Example",Charset.defaultCharset());

         // print the xml.
         while (fis.available() > 0) {
            System.out.print("" + (char) fis.read());
         }
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

<?xml version="1.0" encoding="windows-1252"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Properties Example</comment>
<entry key="Height">200</entry>
<entry key="Width">15</entry>
</properties>
java_util_properties.htm
Advertisements