3:09 pm - Monday October 21, 0593

Properties example program using void store(Writer writer, String comments)

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

           This Properties example program is used 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.

Example Program


import java.util.*;

import java.io.*;

public class PropStoreWriter {

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

		Properties prop = new Properties();

		StringWriter swrit = new StringWriter();

		prop.put("Dhoni", "Cricket");

		prop.setProperty("Nadal", "Tennis");

		prop.setProperty("cena", "wwe");

		System.out.println(prop);

		prop.store(swrit, "success");

		System.out.println("" + swrit);

	}

}

 

Output

{cena=wwe, Nadal=Tennis, Dhoni=Cricket}

#success

#Wed Sep 03 17:01:29 IST 2014

cena=wwe

Nadal=Tennis

Dhoni=Cricket

 

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

 

BACK

Filed in: Z-A All Java Codes

Comments are closed.