Package java.util
Class Properties
- java.lang.Object
-
- java.util.Dictionary<K,V>
-
- java.util.Hashtable<Object,Object>
-
- java.util.Properties
-
- All Implemented Interfaces:
Serializable,Cloneable,Map<Object,Object>
- Direct Known Subclasses:
Provider
public class Properties extends Hashtable<Object,Object>
APropertiesobject is aHashtablewhere the keys and values must beStrings. Each property can have a defaultPropertieslist which specifies the default values to be used when a given key is not found in thisPropertiesinstance.Character Encoding
Note that in some cases
Propertiesuses ISO-8859-1 instead of UTF-8. ISO-8859-1 is only capable of representing a tiny subset of Unicode. Use either theloadFromXML/storeToXMLmethods (which use UTF-8 by default) or theload/storeoverloads that take anOutputStreamWriter(so you can supply a UTF-8 instance) instead.- See Also:
Hashtable,System.getProperties(), Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected PropertiesdefaultsThe default values for keys not found in thisPropertiesinstance.
-
Constructor Summary
Constructors Constructor Description Properties()Constructs a newPropertiesobject.Properties(Properties properties)Constructs a newPropertiesobject using the specified defaultProperties.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description StringgetProperty(String name)Searches for the property with the specified name.StringgetProperty(String name, String defaultValue)Searches for the property with the specified name.voidlist(PrintStream out)Lists the mappings in thisPropertiestooutin a human-readable form.voidlist(PrintWriter out)Lists the mappings in thisPropertiestooutin a human-readable form.voidload(InputStream in)Loads properties from the specifiedInputStream, assumed to be ISO-8859-1.voidload(Reader in)Loads properties from the specifiedReader.voidloadFromXML(InputStream in)Loads the properties from anInputStreamcontaining the properties in XML form.Enumeration<?>propertyNames()Returns all of the property names (keys) in thisPropertiesobject.voidsave(OutputStream out, String comment)Deprecated.This method ignores anyIOExceptionthrown while writing — usestore(java.io.OutputStream, java.lang.String)instead for better exception handling.ObjectsetProperty(String name, String value)Maps the specified key to the specified value.voidstore(OutputStream out, String comment)Stores properties to the specifiedOutputStream, using ISO-8859-1.voidstore(Writer writer, String comment)Stores the mappings in thisPropertiesobject toout, putting the specified comment at the beginning.voidstoreToXML(OutputStream os, String comment)Writes all properties stored in this instance into theOutputStreamin XML representation.voidstoreToXML(OutputStream os, String comment, String encoding)Writes all properties stored in this instance into theOutputStreamin XML representation.Set<String>stringPropertyNames()Returns those property names (keys) in thisPropertiesobject for which both key and value are strings.
-
-
-
Field Detail
-
defaults
protected Properties defaults
The default values for keys not found in thisPropertiesinstance.
-
-
Constructor Detail
-
Properties
public Properties()
Constructs a newPropertiesobject.
-
Properties
public Properties(Properties properties)
Constructs a newPropertiesobject using the specified defaultProperties.- Parameters:
properties- the defaultProperties.
-
-
Method Detail
-
getProperty
public String getProperty(String name)
Searches for the property with the specified name. If the property is not found, the defaultPropertiesare checked. If the property is not found in the defaultProperties,nullis returned.- Parameters:
name- the name of the property to find.- Returns:
- the named property value, or
nullif it can't be found.
-
getProperty
public String getProperty(String name, String defaultValue)
Searches for the property with the specified name. If the property is not found, it looks in the defaultProperties. If the property is not found in the defaultProperties, it returns the specified default.- Parameters:
name- the name of the property to find.defaultValue- the default value.- Returns:
- the named property value.
-
list
public void list(PrintStream out)
Lists the mappings in thisPropertiestooutin a human-readable form. Note that values are truncated to 37 characters, so this method is rarely useful.
-
list
public void list(PrintWriter out)
Lists the mappings in thisPropertiestooutin a human-readable form. Note that values are truncated to 37 characters, so this method is rarely useful.
-
load
public void load(InputStream in) throws IOException
Loads properties from the specifiedInputStream, assumed to be ISO-8859-1. See "Character Encoding".- Parameters:
in- theInputStream- Throws:
IOException
-
load
public void load(Reader in) throws IOException
Loads properties from the specifiedReader. 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- theReader- Throws:
IOException- Since:
- 1.6
-
propertyNames
public Enumeration<?> propertyNames()
Returns all of the property names (keys) in thisPropertiesobject.
-
stringPropertyNames
public Set<String> stringPropertyNames()
Returns those property names (keys) in thisPropertiesobject for which both key and value are strings.- Returns:
- a set of keys in the property list
- Since:
- 1.6
-
save
@Deprecated public void save(OutputStream out, String comment)
Deprecated.This method ignores anyIOExceptionthrown while writing — usestore(java.io.OutputStream, java.lang.String)instead for better exception handling.Saves the mappings in thisPropertiesto the specifiedOutputStream, putting the specified comment at the beginning. The output from this method is suitable for being read by theload(InputStream)method.- Parameters:
out- theOutputStreamto write to.comment- the comment to add at the beginning.- Throws:
ClassCastException- if the key or value of a mapping is not a String.
-
setProperty
public 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 benull.- Parameters:
name- the key.value- the value.- Returns:
- the old value mapped to the key, or
null.
-
store
public void store(OutputStream out, String comment) throws IOException
Stores properties to the specifiedOutputStream, using ISO-8859-1. See "Character Encoding".- Parameters:
out- theOutputStreamcomment- an optional comment to be written, or null- Throws:
IOExceptionClassCastException- if a key or value is not a string
-
store
public void store(Writer writer, String comment) throws IOException
Stores the mappings in thisPropertiesobject toout, putting the specified comment at the beginning.- Parameters:
writer- theWritercomment- an optional comment to be written, or null- Throws:
IOExceptionClassCastException- if a key or value is not a string- Since:
- 1.6
-
loadFromXML
public void loadFromXML(InputStream in) throws IOException, InvalidPropertiesFormatException
Loads the properties from anInputStreamcontaining 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- 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.
-
storeToXML
public void storeToXML(OutputStream os, String comment) throws IOException
Writes all properties stored in this instance into theOutputStreamin 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. TheOutputStreamis not closed at the end. A call to this method is the same as a call tostoreToXML(os, comment, "UTF-8").- Parameters:
os- theOutputStreamto write to.comment- the comment to add. If null, no comment is added.- Throws:
IOException- if an error occurs during writing to the output.
-
storeToXML
public void storeToXML(OutputStream os, String comment, String encoding) throws IOException
Writes all properties stored in this instance into theOutputStreamin 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 parameterencodingdefines which encoding should be used. TheOutputStreamis not closed at the end.- Parameters:
os- theOutputStreamto write to.comment- the comment to add. If null, no comment is added.encoding- the code identifying the encoding that should be used to write into theOutputStream.- Throws:
IOException- if an error occurs during writing to the output.
-
-