1 *java.util.Properties* *Properties* The Properties class represents a persistent
3 public class Properties
4 extends |java.util.Hashtable|
6 |java.util.Properties_Description|
7 |java.util.Properties_Fields|
8 |java.util.Properties_Constructors|
9 |java.util.Properties_Methods|
11 ================================================================================
13 *java.util.Properties_Fields*
14 |java.util.Properties_java.util.Properties.defaults|
16 *java.util.Properties_Constructors*
17 |java.util.Properties()|Creates an empty property list with no default values.
18 |java.util.Properties(Properties)|Creates an empty property list with the speci
20 *java.util.Properties_Methods*
21 |java.util.Properties.getProperty(String)|Searches for the property with the sp
22 |java.util.Properties.getProperty(String,String)|Searches for the property with
23 |java.util.Properties.list(PrintStream)|Prints this property list out to the sp
24 |java.util.Properties.list(PrintWriter)|Prints this property list out to the sp
25 |java.util.Properties.load(InputStream)|Reads a property list (key and element
26 |java.util.Properties.load(Reader)|Reads a property list (key and element pairs
27 |java.util.Properties.loadFromXML(InputStream)|Loads all of the properties repr
28 |java.util.Properties.propertyNames()|Returns an enumeration of all the keys in
29 |java.util.Properties.save(OutputStream,String)|Calls the store(OutputStream ou
30 |java.util.Properties.setProperty(String,String)|Calls the Hashtable method put
31 |java.util.Properties.store(OutputStream,String)|Writes this property list (key
32 |java.util.Properties.store(Writer,String)|Writes this property list (key and e
33 |java.util.Properties.storeToXML(OutputStream,String)|Emits an XML document rep
34 |java.util.Properties.storeToXML(OutputStream,String,String)|Emits an XML docum
35 |java.util.Properties.stringPropertyNames()|Returns a set of keys in this prope
37 *java.util.Properties_Description*
39 The Properties class represents a persistent set of properties. The Properties
40 can be saved to a stream or loaded from a stream. Each key and its
41 corresponding value in the property list is a string.
43 A property list can contain another property list as its "defaults"; this
44 second property list is searched if the property key is not found in the
45 original property list.
47 Because Properties inherits from Hashtable, the put and putAll methods can be
48 applied to a Properties object. Their use is strongly discouraged as they allow
49 the caller to insert entries whose keys or values are not Strings. The
50 setProperty method should be used instead. If the store or save method is
51 called on a "compromised" Properties object that contains a non-String key or
52 value, the call will fail. Similarly, the call to the propertyNames or list
53 method will fail if it is called on a "compromised" Properties object that
54 contains a non-String key.
56 The load(Reader)(|java.util.Properties|) / store(Writer,
57 String)(|java.util.Properties|) methods load and store properties from and to a
58 character based stream in a simple line-oriented format specified below.
60 The load(InputStream)(|java.util.Properties|) / store(OutputStream,
61 String)(|java.util.Properties|) methods work the same way as the
62 load(Reader)/store(Writer, String) pair, except the input/output stream is
63 encoded in ISO 8859-1 character encoding. Characters that cannot be directly
64 represented in this encoding can be written using Unicode escapes ; only a
65 single 'u' character is allowed in an escape sequence. The native2ascii tool
66 can be used to convert property files to and from other character encodings.
68 The (|java.util.Properties|) and (|java.util.Properties|) methods load and
69 store properties in a simple XML format. By default the UTF-8 character
70 encoding is used, however a specific encoding may be specified if required. An
71 XML properties document has the following DOCTYPE declaration:
75 <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
77 Note that the system URI (http://java.sun.com/dtd/properties.dtd) is not
78 accessed when exporting or importing properties; it merely serves as a string
79 to uniquely identify the DTD, which is:
81 <?xml version="1.0" encoding="UTF-8"?>
83 <!-- DTD for properties -->
85 <!ELEMENT properties ( comment?, entry* ) >
87 <!ATTLIST properties version CDATA #FIXED "1.0">
89 <!ELEMENT comment (#PCDATA) >
91 <!ELEMENT entry (#PCDATA) >
93 <!ATTLIST entry key CDATA #REQUIRED>
97 *java.util.Properties_java.util.Properties.defaults*
99 A property list that contains default values for any keys not found in this
104 *java.util.Properties()*
108 Creates an empty property list with no default values.
111 *java.util.Properties(Properties)*
113 public Properties(java.util.Properties defaults)
115 Creates an empty property list with the specified defaults.
117 defaults - the defaults.
119 *java.util.Properties.getProperty(String)*
121 public |java.lang.String| getProperty(java.lang.String key)
123 Searches for the property with the specified key in this property list. If the
124 key is not found in this property list, the default property list, and its
125 defaults, recursively, are then checked. The method returns null if the
126 property is not found.
129 key - the property key.
131 Returns: the value in this property list with the specified key value.
133 *java.util.Properties.getProperty(String,String)*
135 public |java.lang.String| getProperty(
136 java.lang.String key,
137 java.lang.String defaultValue)
139 Searches for the property with the specified key in this property list. If the
140 key is not found in this property list, the default property list, and its
141 defaults, recursively, are then checked. The method returns the default value
142 argument if the property is not found.
145 key - the hashtable key.
146 defaultValue - a default value.
148 Returns: the value in this property list with the specified key value.
150 *java.util.Properties.list(PrintStream)*
152 public void list(java.io.PrintStream out)
154 Prints this property list out to the specified output stream. This method is
155 useful for debugging.
158 out - an output stream.
160 *java.util.Properties.list(PrintWriter)*
162 public void list(java.io.PrintWriter out)
164 Prints this property list out to the specified output stream. This method is
165 useful for debugging.
168 out - an output stream.
170 *java.util.Properties.load(InputStream)*
172 public synchronized void load(java.io.InputStream inStream)
173 throws |java.io.IOException|
175 Reads a property list (key and element pairs) from the input byte stream. The
176 input stream is in a simple line-oriented format as specified in
177 load(Reader)(|java.util.Properties|) and is assumed to use the ISO 8859-1
178 character encoding; that is each byte is one Latin1 character. Characters not
179 in Latin1, and certain special characters, are represented in keys and elements
180 using Unicode escapes.
182 The specified stream remains open after this method returns.
185 inStream - the input stream.
187 *java.util.Properties.load(Reader)*
189 public synchronized void load(java.io.Reader reader)
190 throws |java.io.IOException|
192 Reads a property list (key and element pairs) from the input character stream
193 in a simple line-oriented format.
195 Properties are processed in terms of lines. There are two kinds of line,
196 natural lines and logical lines. A natural line is defined as a line of
197 characters that is terminated either by a set of line terminator characters (\n
198 or \r or \r\n) or by the end of the stream. A natural line may be either a
199 blank line, a comment line, or hold all or some of a key-element pair. A
200 logical line holds all the data of a key-element pair, which may be spread out
201 across several adjacent natural lines by escaping the line terminator sequence
202 with a backslash character \. Note that a comment line cannot be extended in
203 this manner; every natural line that is a comment must have its own comment
204 indicator, as described below. Lines are read from input until the end of the
207 A natural line that contains only white space characters is considered blank
208 and is ignored. A comment line has an ASCII '#' or '!' as its first non-white
209 space character; comment lines are also ignored and do not encode key-element
210 information. In addition to line terminators, this format considers the
211 characters space (' ', 'u0020'), tab ('\t', 'u0009'), and form feed ('\f',
212 'u000C') to be white space.
214 If a logical line is spread across several natural lines, the backslash
215 escaping the line terminator sequence, the line terminator sequence, and any
216 white space at the start of the following line have no affect on the key or
217 element values. The remainder of the discussion of key and element parsing
218 (when loading) will assume all the characters constituting the key and element
219 appear on a single natural line after line continuation characters have been
220 removed. Note that it is not sufficient to only examine the character preceding
221 a line terminator sequence to decide if the line terminator is escaped; there
222 must be an odd number of contiguous backslashes for the line terminator to be
223 escaped. Since the input is processed from left to right, a non-zero even
224 number of 2n contiguous backslashes before a line terminator (or elsewhere)
225 encodes n backslashes after escape processing.
227 The key contains all of the characters in the line starting with the first
228 non-white space character and up to, but not including, the first unescaped
229 '=', ':', or white space character other than a line terminator. All of these
230 key termination characters may be included in the key by escaping them with a
231 preceding backslash character; for example,
235 would be the two-character key ":=". Line terminator characters can be included
236 using \r and \n escape sequences. Any white space after the key is skipped; if
237 the first non-white space character after the key is '=' or ':', then it is
238 ignored and any white space characters after it are also skipped. All remaining
239 characters on the line become part of the associated element string; if there
240 are no remaining characters, the element is the empty string . Once the raw
241 character sequences constituting the key and element are identified, escape
242 processing is performed as described above.
244 As an example, each of the following three lines specifies the key "Truth" and
245 the associated element value "Beauty":
249 Truth = Beauty Truth:Beauty Truth :Beauty
251 As another example, the following three lines specify a single property:
255 fruits apple, banana, pear, \ cantaloupe, watermelon, \ kiwi, mango
257 The key is "fruits" and the associated element is:
259 "apple, banana, pear, cantaloupe, watermelon, kiwi, mango"
261 Note that a space appears before each \ so that a space will appear after each
262 comma in the final result; the \, line terminator, and leading white space on
263 the continuation line are merely discarded and are not replaced by one or more
266 As a third example, the line:
270 specifies that the key is "cheeses" and the associated element is the empty
273 Characters in keys and elements can be represented in escape sequences similar
274 to those used for character and string literals (see 3.3 and 3.10.6 of the Java
275 Language Specification).
277 The differences from the character escape sequences and Unicode escapes used
278 for characters and strings are:
280 Octal escapes are not recognized.
282 The character sequence \b does not represent a backspace character.
284 The method does not treat a backslash character, \, before a non-valid escape
285 character as an error; the backslash is silently dropped. For example, in a
286 Java string the sequence "\z" would cause a compile time error. In contrast,
287 this method silently drops the backslash. Therefore, this method treats the two
288 character sequence "\b" as equivalent to the single character 'b'.
290 Escapes are not necessary for single and double quotes; however, by the rule
291 above, single and double quote characters preceded by a backslash still yield
292 single and double quote characters, respectively.
294 Only a single 'u' character is allowed in a Uniocde escape sequence.
298 The specified stream remains open after this method returns.
301 reader - the input character stream.
303 *java.util.Properties.loadFromXML(InputStream)*
305 public synchronized void loadFromXML(java.io.InputStream in)
306 throws |java.util.InvalidPropertiesFormatException|
307 |java.io.IOException|
309 Loads all of the properties represented by the XML document on the specified
310 input stream into this properties table.
312 The XML document must have the following DOCTYPE declaration:
314 <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
316 Furthermore, the document must satisfy the properties DTD described above.
318 The specified stream is closed after this method returns.
321 in - the input stream from which to read the XML document.
323 *java.util.Properties.propertyNames()*
325 public |java.util.Enumeration|<?> propertyNames()
327 Returns an enumeration of all the keys in this property list, including
328 distinct keys in the default property list if a key of the same name has not
329 already been found from the main properties list.
333 Returns: an enumeration of all the keys in this property list, including the keys in the
334 default property list.
336 *java.util.Properties.save(OutputStream,String)*
338 public synchronized void save(
339 java.io.OutputStream out,
340 java.lang.String comments)
342 Calls the store(OutputStream out, String comments) method and suppresses
343 IOExceptions that were thrown.
345 Deprecated: This method does not throw an IOException if an I/O error
346 occurs while saving the property list. The preferred way to save a
347 properties list is via the <code>store(OutputStream out,
348 String comments)</code> method or the
349 <code>storeToXML(OutputStream os, String comment)</code> method.
351 out - an output stream.
352 comments - a description of the property list.
354 *java.util.Properties.setProperty(String,String)*
356 public synchronized |java.lang.Object| setProperty(
357 java.lang.String key,
358 java.lang.String value)
360 Calls the Hashtable method put. Provided for parallelism with the getProperty
361 method. Enforces use of strings for property keys and values. The value
362 returned is the result of the Hashtable call to put.
365 key - the key to be placed into this property list.
366 value - the value corresponding to key.
368 Returns: the previous value of the specified key in this property list, or null if it
371 *java.util.Properties.store(OutputStream,String)*
374 java.io.OutputStream out,
375 java.lang.String comments)
376 throws |java.io.IOException|
378 Writes this property list (key and element pairs) in this Properties table to
379 the output stream in a format suitable for loading into a Properties table
380 using the load(InputStream)(|java.util.Properties|) method.
382 Properties from the defaults table of this Properties table (if any) are not
383 written out by this method.
385 This method outputs the comments, properties keys and values in the same format
386 as specified in store(Writer)(|java.util.Properties|) , with the following
389 The stream is written using the ISO 8859-1 character encoding.
391 Characters not in Latin-1 in the comments are written as uxxxx for their
392 appropriate unicode hexadecimal value xxxx.
394 Characters less than u0020 and characters greater than u007E in property keys
395 or values are written as uxxxx for the appropriate hexadecimal value xxxx.
397 After the entries have been written, the output stream is flushed. The output
398 stream remains open after this method returns.
401 out - an output stream.
402 comments - a description of the property list.
404 *java.util.Properties.store(Writer,String)*
407 java.io.Writer writer,
408 java.lang.String comments)
409 throws |java.io.IOException|
411 Writes this property list (key and element pairs) in this Properties table to
412 the output character stream in a format suitable for using the
413 load(Reader)(|java.util.Properties|) method.
415 Properties from the defaults table of this Properties table (if any) are not
416 written out by this method.
418 If the comments argument is not null, then an ASCII # character, the comments
419 string, and a line separator are first written to the output stream. Thus, the
420 comments can serve as an identifying comment. Any one of a line feed ('\n'), a
421 carriage return ('\r'), or a carriage return followed immediately by a line
422 feed in comments is replaced by a line separator generated by the Writer and if
423 the next character in comments is not character # or character ! then an ASCII
424 # is written out after that line separator.
426 Next, a comment line is always written, consisting of an ASCII # character, the
427 current date and time (as if produced by the toString method of Date for the
428 current time), and a line separator as generated by the Writer.
430 Then every entry in this Properties table is written out, one per line. For
431 each entry the key string is written, then an ASCII =, then the associated
432 element string. For the key, all space characters are written with a preceding
433 \ character. For the element, leading space characters, but not embedded or
434 trailing space characters, are written with a preceding \ character. The key
435 and element characters #, !, =, and : are written with a preceding backslash to
436 ensure that they are properly loaded.
438 After the entries have been written, the output stream is flushed. The output
439 stream remains open after this method returns.
442 writer - an output character stream writer.
443 comments - a description of the property list.
445 *java.util.Properties.storeToXML(OutputStream,String)*
447 public synchronized void storeToXML(
448 java.io.OutputStream os,
449 java.lang.String comment)
450 throws |java.io.IOException|
452 Emits an XML document representing all of the properties contained in this
455 An invocation of this method of the form props.storeToXML(os, comment) behaves
456 in exactly the same way as the invocation props.storeToXML(os, comment,
460 os - the output stream on which to emit the XML document.
461 comment - a description of the property list, or null if no comment is desired.
463 *java.util.Properties.storeToXML(OutputStream,String,String)*
465 public synchronized void storeToXML(
466 java.io.OutputStream os,
467 java.lang.String comment,
468 java.lang.String encoding)
469 throws |java.io.IOException|
471 Emits an XML document representing all of the properties contained in this
472 table, using the specified encoding.
474 The XML document will have the following DOCTYPE declaration:
476 <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
478 If the specified comment is null then no comment will be stored in the
481 The specified stream remains open after this method returns.
484 os - the output stream on which to emit the XML document.
485 comment - a description of the property list, or null if no comment is desired.
487 *java.util.Properties.stringPropertyNames()*
489 public |java.util.Set|<String> stringPropertyNames()
491 Returns a set of keys in this property list where the key and its corresponding
492 value are strings, including distinct keys in the default property list if a
493 key of the same name has not already been found from the main properties list.
494 Properties whose key or value is not of type String are omitted.
496 The returned set is not backed by the Properties object. Changes to this
497 Properties are not reflected in the set, or vice versa.
501 Returns: a set of keys in this property list where the key and its corresponding value
502 are strings, including the keys in the default property list.