digitization, transformation, binary

Class Properties in Java

The Properties class is a subclass of Hashtable that deals exclusively with string keys and string values. Furthermore, a Properties object can be written to an OutputStream and read from an InputStream. Note that the load() and save() correctly convert Unicode strings to and from byte streams, using the getLocalizedInputStream() and getLocalizedOutputStream() methods of Runtime.

The properties class represents a set of properties. The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list is a String. A property list can contain another property list as its default. In case of search for a key, the second list is searched if the property key is not found in the original property list.

Properties class is an extension of Hashtable that allows key/value pairs to be read from and written to a stream. The Properties class is used to implement the system properties list, which supports user customization by allowing programs to look up the value of named resources.

When We create a Properties object, we need to specify another Properties object that contains default values. Keys (property names) and values are associated with a Properties object with the Hashtable method put().

Values are looked up with getProperty()–if this method does not find the key in the current Properties object, it looks in the default Properties object that was passed to the constructor method. A default value may also be specified in case the key is not found at all.

propertyNames() returns an enumeration of all property names (keys) stored in the Properties object and (recursively) also all property names stored in the default Properties object associated with it.

READ  Class URLEncoder in Java

list() prints the properties stored in a Properties object. It is useful for debugging.
save() writes a Properties object to a stream.

load() reads key/value pairs from a stream and stores them in a Properties object.

The structure of the Class Property is defined as:

public class java.util.Properties extends java.util.Hashtable{
//member element
protected properties defaults;//A property list which contains default values for any keys not found in the current property list
//constructors
public Properties();//creates a an empty Property list with no default values.
public Properties(Properties defaults);//creates a an empty Property list with default values specified in the argument.
//methods:
public String getProperty(String key);
public String getProperties(string key,String defaultValue);
public void list(PrintStream out);
public void list(PrintWriter out);
public synchronized void Load(InputStream in)throws IOException;
public synchronized void save(OutputStream out,String header);
public Enumeration PropertiesNames();
} 

The details of the class structure are given as follows:

protected Properties defaults;

protected Properties defaults represent a collection of default property values. If a key/value pair is not found in this Properties object, the default object is searched.

public Properties();

public Properties() constructor creates an empty Properties object.

public Properties(Properties defaults);

public Properties(Properties defaults) method constructor creates an empty Properties object that gets default values for keys that it does not contain from the given Properties object.

Parameter
defaults – A set of default key/value pairs.

public String getProperty(String key);

public String getProperty(String key) method returns the value that is associated with the given key. If the key is not found, a default value is returned if this object was created with a default Properties table that contains a value for the key. If neither a value nor a default value can be found, this method returns null.

READ  Class UnknownHostException in Java

This method returns the value of the given property or null if the key is not associated with any value.

Parameter
key – The key of the value to retrieve.

public String getProperty(String key, String defaultValue);

public String getProperty(String key, String defaultValue) method returns the value that is associated with the given key. If the key is not found, a default value is returned if this object was created with a default Properties table that contains a value for the key. If neither a value nor a default value can be found, this method returns
defaultValue.

This method returns the value of the given property or defaultValue if the key is not associated with any value.

Parameter
key – The key of the value to retrieve.
defaultValue – The value to return if key cannot be found.

public void list(PrintStream out);

public void list(PrintStream out) method writes a listing of the contents of this object, in a format suitable for debugging, to the given PrintStream. As of JDK 1.1, use list(PrintWriter) instead.

Parameter
out – The output stream to use.

public void list(PrintWriter out);

public void list(PrintWriter out) method writes a listing of the contents of this object, in a format suitable for debugging, to the given PrintWriter.

Parameter
out – The output stream to use.

public synchronized void load(InputStream in);

public synchronized void load(InputStream in) method reads key/value pairs from the given InputStream. Here is the format the method expects:

  • Lines that begin with # or ! are comments and are ignored.
  • Blank lines are ignored.
  • All other lines should specify a key/value pair and be of the form:
READ  Class Exception in Java

key = value
or
key : value
or
key value

All of these forms are equivalent. The method also recognizes the following escape characters and treats them as described:

Character code Meaning and handling
\newline An escaped newline character is ignored, along with the spaces or tabs that follow it
\n Expands to a newline character
\r Expands to a return character
\t Expands to a tab character
\uxxxx Expands to the Unicode character code specified by the hexadecimal digits

 

Parameter
in – the input stream to use.

public Enumeration propertyNames();

public Enumeration propertyNames() method returns an Enumeration that iterates through the keys in this Properties object.

This method returns the keys in this Properties object as an Enumeration.

public synchronized void save(OutputStream out, String header);

public synchronized void save(OutputStream out, String header) method writes key/value pairs to the given OutputStream. The format of the output is such that it can be read by the load() method.If the header is not null, a # followed by the header is written to the OutputStream first, thereby making the content of the string a comment that precedes the key/value pairs.

Parameter
out – The output stream to use.
header – A header string.

Apart from these Properties class also has inherited methods from class- Object. They are as follows:

  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • wait(long)

Properties class also has inherited methods from Hashtable. They are as follows:

  • clear()
  • contains(Object)
  • elements()
  • isEmpty()
  • put(Object, Object)
  • size()
  • clone()
  • containsKey(Object)
  • get(Object)
  • keys()
  • remove(Object)
  • toString()

LEAVE A REPLY

Please enter your comment!
Please enter your name here