Java.util.Properties.store() Method

Advertisements


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.

Declaration

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

public void store(Writer writer,String comments)

Parameters

  • out -- an output character stream writer.

  • comments -- a description of the property list.

Return Value

This method returns the previous value of the specified key in this property list, or null if it did not have one.

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.

Example

The following example shows the usage of java.util.Properties.store() method.

package com.tutorialspoint;

import java.io.IOException;
import java.io.StringWriter;
import java.util.*;

public class PropertiesDemo {

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

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

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

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

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

{Width=1500, Height=200}
#PropertiesDemo
#Sat Jun 30 02:34:42 EEST 2012
Width=1500
Height=200



Advertisements
Advertisements