Properties void store(OutputStream out,String comments) method Example Program


Writes this property list (key and element pairs) in this Properties table to the output stream in a format suitable for loading into a Properties table using the load(InputStream) method.Properties from the defaults table of this Properties table (if any) are not written out by this method.

Program

package com.candidjava;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
/* 
 * @author : Mohan 
 * @description : The java.util.Properties.storeToXML(OutputStream osString comment) method Emits an XML document representing all of the properties contained in this table. 
 */
public class PropertiesStoreToXml {
	public static void main(String[] args) {
		Properties prop = new Properties();

		// add some properties
		prop.put("kamal", "1");
		prop.put("aravind", "2");

		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

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Properties Example</comment>
<entry key="kamal">1</entry>
<entry key="aravind">2</entry>
</properties>

Explanation

public void store(OutputStream out,String comments) throws IOException
Writes this property list (key and element pairs) in this Properties table to the output stream in a format suitable for loading into a Properties table using the load(InputStream) method.
Properties from the defaults table of this Properties table (if any) are not written out by this method.
This method outputs the comments, properties keys and values in the same format as specified in store(Writer), with the following differences:
The stream is written using the ISO 8859-1 character encoding.
Characters not in Latin-1 in the comments are written as \uxxxx for their appropriate unicode hexadecimal value xxxx.
Characters less than \u0020 and characters greater than \u007E in property keys or values are written as \uxxxx for the appropriate hexadecimal value xxxx.
After the entries have been written, the output stream is flushed. The output stream remains open after this method returns.

Parameters:
out - an output stream.
comments - a description of the property list.
Throws:
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.
Since:
1.2


Related Post

Comments


©candidjava.com