Skip to content

Most visited

Recently visited

navigation
Added in API level 1

Properties

public class Properties
extends Hashtable<ObjectObject>

java.lang.Object
   ↳ java.util.Dictionary<java.lang.Objectjava.lang.Object>
     ↳ java.util.Hashtable<java.lang.Objectjava.lang.Object>
       ↳ java.util.Properties
Known Direct Subclasses
Known Indirect Subclasses


A Properties object is a Hashtable where the keys and values must be Strings. Each property can have a default Properties list which specifies the default values to be used when a given key is not found in this Properties instance.

Character Encoding

Note that in some cases Properties uses ISO-8859-1 instead of UTF-8. ISO-8859-1 is only capable of representing a tiny subset of Unicode. Use either the loadFromXML/storeToXML methods (which use UTF-8 by default) or the load/store overloads that take an OutputStreamWriter (so you can supply a UTF-8 instance) instead.

See also:

Summary

Fields

protected Properties defaults

The default values for keys not found in this Properties instance.

Public constructors

Properties()

Constructs a new Properties object.

Properties(Properties properties)

Constructs a new Properties object using the specified default Properties.

Public methods

String getProperty(String name, String defaultValue)

Searches for the property with the specified name.

String getProperty(String name)

Searches for the property with the specified name.

void list(PrintStream out)

Lists the mappings in this Properties to out in a human-readable form.

void list(PrintWriter out)

Lists the mappings in this Properties to out in a human-readable form.

void load(InputStream in)

Loads properties from the specified InputStream, assumed to be ISO-8859-1.

void load(Reader in)

Loads properties from the specified Reader.

void loadFromXML(InputStream in)

Loads the properties from an InputStream containing the properties in XML form.

Enumeration<?> propertyNames()

Returns all of the property names (keys) in this Properties object.

void save(OutputStream out, String comment)

This method was deprecated in API level 1. This method ignores any IOException thrown while writing — use store(OutputStream, String) instead for better exception handling.

Object setProperty(String name, String value)

Maps the specified key to the specified value.

void store(OutputStream out, String comment)

Stores properties to the specified OutputStream, using ISO-8859-1.

void store(Writer writer, String comment)

Stores the mappings in this Properties object to out, putting the specified comment at the beginning.

void storeToXML(OutputStream os, String comment, String encoding)

Writes all properties stored in this instance into the OutputStream in XML representation.

void storeToXML(OutputStream os, String comment)

Writes all properties stored in this instance into the OutputStream in XML representation.

Set<String> stringPropertyNames()

Returns those property names (keys) in this Properties object for which both key and value are strings.

Inherited methods

From class java.util.Hashtable
From class java.util.Dictionary
From class java.lang.Object
From interface java.util.Map

Fields

defaults

Added in API level 1
Properties defaults

The default values for keys not found in this Properties instance.

Public constructors

Properties

Added in API level 1
Properties ()

Constructs a new Properties object.

Properties

Added in API level 1
Properties (Properties properties)

Constructs a new Properties object using the specified default Properties.

Parameters
properties Properties: the default Properties.

Public methods

getProperty

Added in API level 1
String getProperty (String name, 
                String defaultValue)

Searches for the property with the specified name. If the property is not found, it looks in the default Properties. If the property is not found in the default Properties, it returns the specified default.

Parameters
name String: the name of the property to find.
defaultValue String: the default value.
Returns
String the named property value.

getProperty

Added in API level 1
String getProperty (String name)

Searches for the property with the specified name. If the property is not found, the default Properties are checked. If the property is not found in the default Properties, null is returned.

Parameters
name String: the name of the property to find.
Returns
String the named property value, or null if it can't be found.

list

Added in API level 1
void list (PrintStream out)

Lists the mappings in this Properties to out in a human-readable form. Note that values are truncated to 37 characters, so this method is rarely useful.

Parameters
out PrintStream

list

Added in API level 1
void list (PrintWriter out)

Lists the mappings in this Properties to out in a human-readable form. Note that values are truncated to 37 characters, so this method is rarely useful.

Parameters
out PrintWriter

load

Added in API level 1
void load (InputStream in)

Loads properties from the specified InputStream, assumed to be ISO-8859-1. See "Character Encoding".

Parameters
in InputStream: the InputStream
Throws
IOException

load

Added in API level 9
void load (Reader in)

Loads properties from the specified Reader. The properties file is interpreted according to the following rules:

  • Empty lines are ignored.
  • Lines starting with either a "#" or a "!" are comment lines and are ignored.
  • A backslash at the end of the line escapes the following newline character ("\r", "\n", "\r\n"). If there's whitespace after the backslash it will just escape that whitespace instead of concatenating the lines. This does not apply to comment lines.
  • A property line consists of the key, the space between the key and the value, and the value. The key goes up to the first whitespace, "=" or ":" that is not escaped. The space between the key and the value contains either one whitespace, one "=" or one ":" and any amount of additional whitespace before and after that character. The value starts with the first character after the space between the key and the value.
  • Following escape sequences are recognized: "\ ", "\\", "\r", "\n", "\!", "\#", "\t", "\b", "\f", and "\uXXXX" (unicode character).

Parameters
in Reader: the Reader
Throws
IOException
IOException

loadFromXML

Added in API level 1
void loadFromXML (InputStream in)

Loads the properties from an InputStream containing the properties in XML form. The XML document must begin with (and conform to) following DOCTYPE:

 <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
 
Also the content of the XML data must satisfy the DTD but the xml is not validated against it. The DTD is not loaded from the SYSTEM ID. After this method returns the InputStream is not closed.

Parameters
in InputStream: the InputStream containing the XML document.
Throws
IOException in case an error occurs during a read operation.
InvalidPropertiesFormatException if the XML data is not a valid properties file.

propertyNames

Added in API level 1
Enumeration<?> propertyNames ()

Returns all of the property names (keys) in this Properties object.

Returns
Enumeration<?>

save

Added in API level 1
void save (OutputStream out, 
                String comment)

This method was deprecated in API level 1.
This method ignores any IOException thrown while writing — use store(OutputStream, String) instead for better exception handling.

Saves the mappings in this Properties to the specified OutputStream, putting the specified comment at the beginning. The output from this method is suitable for being read by the load(InputStream) method.

Parameters
out OutputStream: the OutputStream to write to.
comment String: the comment to add at the beginning.
Throws
ClassCastException if the key or value of a mapping is not a String.

setProperty

Added in API level 1
Object setProperty (String name, 
                String value)

Maps the specified key to the specified value. If the key already exists, the old value is replaced. The key and value cannot be null.

Parameters
name String: the key.
value String: the value.
Returns
Object the old value mapped to the key, or null.

store

Added in API level 1
void store (OutputStream out, 
                String comment)

Stores properties to the specified OutputStream, using ISO-8859-1. See "Character Encoding".

Parameters
out OutputStream: the OutputStream
comment String: an optional comment to be written, or null
Throws
IOException
ClassCastException if a key or value is not a string
IOException

store

Added in API level 9
void store (Writer writer, 
                String comment)

Stores the mappings in this Properties object to out, putting the specified comment at the beginning.

Parameters
writer Writer: the Writer
comment String: an optional comment to be written, or null
Throws
IOException
ClassCastException if a key or value is not a string
IOException

storeToXML

Added in API level 1
void storeToXML (OutputStream os, 
                String comment, 
                String encoding)

Writes all properties stored in this instance into the OutputStream in XML representation. The DOCTYPE is

 <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
 
If the comment is null, no comment is added to the output. The parameter encoding defines which encoding should be used. The OutputStream is not closed at the end.

Parameters
os OutputStream: the OutputStream to write to.
comment String: the comment to add. If null, no comment is added.
encoding String: the code identifying the encoding that should be used to write into the OutputStream.
Throws
IOException if an error occurs during writing to the output.

storeToXML

Added in API level 1
void storeToXML (OutputStream os, 
                String comment)

Writes all properties stored in this instance into the OutputStream in XML representation. The DOCTYPE is

 <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
 
If the comment is null, no comment is added to the output. UTF-8 is used as the encoding. The OutputStream is not closed at the end. A call to this method is the same as a call to storeToXML(os, comment, "UTF-8").

Parameters
os OutputStream: the OutputStream to write to.
comment String: the comment to add. If null, no comment is added.
Throws
IOException if an error occurs during writing to the output.

stringPropertyNames

Added in API level 9
Set<String> stringPropertyNames ()

Returns those property names (keys) in this Properties object for which both key and value are strings.

Returns
Set<String> a set of keys in the property list
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.