Properties void store(Writer writer,String comments) method Example Program


Writes this property list (key and element pairs) in this Properties table to the output character stream in a format suitable for using the load(Reader) 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.IOException;
import java.io.StringWriter;
import java.util.Properties;
/* 
 * @author : Mohan 
 * @description : The java.util.Properties.store(Writer writer,String comments) method writes this property list (key and element pairs) in this Properties table to the output character stream in a format suitable for using the load(Reader) method. 
 */
public class PropertiesStoreWriter {
	public static void main(String[] args) {
		   Properties prop = new Properties();
		   StringWriter sw = new StringWriter();

		   // add some properties
		   prop.setProperty("anandh", "2");
			prop.put("karthi", "1");

		   // print the list 
		   System.out.println("" + prop);
		   try {
		   // store the properties list in an output writer
		   prop.store(sw, "PropertiesStoreWriter");

		   // print the writer
		   System.out.println("" + sw.toString());
		   } catch (IOException ex) {
		   ex.printStackTrace();
		   }
		   }
}

Output

{karthi=1, anandh=2}
#PropertiesStoreWriter
#Thu Jul 02 18:02:26 IST 2015
karthi=1
anandh=2

Explanation

public void store(Writer writer,String comments)throws IOException
Writes this property list (key and element pairs) in this Properties table to the output character stream in a format suitable for using the load(Reader) method.
Properties from the defaults table of this Properties table (if any) are not written out by this method.
If the comments argument is not null, then an ASCII # character, the comments string, and a line separator are first written to the output stream. Thus, the comments can serve as an identifying comment. Any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a line feed in comments is replaced by a line separator generated by the Writer and if the next character in comments is not character # or character ! then an ASCII # is written out after that line separator.

Next, a comment line is always written, consisting of an ASCII # character, the current date and time (as if produced by the toString method of Date for the current time), and a line separator as generated by the Writer.

Then every entry in this Properties table is written out, one per line. For each entry the key string is written, then an ASCII =, then the associated element string. For the key, all space characters are written with a preceding \ character. For the element, leading space characters, but not embedded or trailing space characters, are written with a preceding \ character. The key and element characters #, !, =, and : are written with a preceding backslash to ensure that they are properly loaded.
After the entries have been written, the output stream is flushed. The output stream remains open after this method returns.

Parameters:
writer - an output character stream writer.
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 writer is null.
Since:
1.6


Related Post

Comments


©candidjava.com