⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 javautil.doc5.html

📁 java语言规范
💻 HTML
字号:
<html>
<head>
<title>The Java Language Specification The Package java.util</title>
</head>
<body BGCOLOR=#eeeeff text=#000000 LINK=#0000ff VLINK=#000077 ALINK=#ff0000>
 
<a href="index.html">Contents</a> | <a href="javautil.doc4.html">Prev</a> | <a href="javautil.doc6.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<hr><br>
 
<a name="23061"></a>
<center><h1>21.6  The Class  <code>java.util.Properties</code></h1></center>
<a name="23062"></a>
A <code>Properties</code> table is a kind of <code>Hashtable</code> with two functionality extensions 
and with the restriction that keys and elements must be strings. First, there are 
methods for reading entries into the table from an input stream and writing all the 
entries in the table to an output stream. Second, a <code>Properties</code> table may refer to 
another <code>Properties</code> table that provides default values. The <code>getProperty</code> 
method is much like the <code>get</code> method <a href="javautil.doc3.html#7512">(&#167;21.4.3)</a>, but if an entry is not found in this 
table, then the defaults table is searched (and that defaults table may itself refer to 
another defaults table, and so on, recursively).
<p><pre><a name="23066"></a>public class <code><b>Properties</b></code> extends Hashtable {
<a name="23067"></a>	protected Properties <code><b>defaults</b></code>;
<a name="23068"></a>	public <code><b>Properties</b></code>();
<a name="23069"></a>	public <code><b>Properties</b></code>(Properties defaults);
<a name="23070"></a>	public String <code><b>getProperty</b></code>(String key);
<a name="23071"></a>	public String <code><b>getProperty</b></code>(String key, String defaultValue);
<a name="23072"></a>	public Enumeration <code><b>propertyNames</b></code>();
<a name="23073"></a>	public void <code><b>load</b></code>(InputStream in) throws IOException;
<a name="23074"></a>	public void <code><b>save</b></code>(OutputStream out, String header);
<a name="23075"></a>	public void <code><b>list</b></code>(PrintStream out);
<a name="23076"></a>}
</pre><a name="23078"></a>
<p><font size=+1><strong>21.6.1   </strong> <code>protected Properties <code><b>defaults</b></code>;</code></font>
<p>
<a name="23079"></a>
If the <code>defaults</code> field is not <code>null</code>, it is another <code>Properties</code> table that provides 
default values for this <code>Properties</code> table.
<p><a name="23080"></a>
<p><font size=+1><strong>21.6.2   </strong> <code>public <code><b>Properties</b></code>()</code></font>
<p>
<a name="23081"></a>
This constructor initializes a newly created <code>Properties</code> table so that it has no 
defaults table. Initially, there are no entries in the newly created table.
<p><a name="23082"></a>
<p><font size=+1><strong>21.6.3   </strong> <code>public <code><b>Properties</b></code>(Properties defaults)</code></font>
<p>
<a name="23083"></a>
This constructor initializes a newly created <code>Properties</code> table so its defaults table 
is <code>defaults</code>. The argument <code>defaults</code> may be <code>null</code>, in which case the newly created
<code>Properties</code> table will not have a defaults table. Initially, there are no entries 
in the newly created table.
<p><a name="23084"></a>
<p><font size=+1><strong>21.6.4   </strong> <code>public String <code><b>getProperty</b></code>(String key)</code></font>
<p>
<a name="23085"></a>
If there is an entry in this <code>Properties</code> table with <code>key</code> as its key, the associated 
element is returned. Otherwise, if this <code>Properties</code> table has a defaults table, then 
whatever its <code>getProperty</code> method returns is returned. Otherwise, <code>null</code> is 
returned.
<p><a name="23086"></a>
<p><font size=+1><strong>21.6.5   </strong> <code>public String <code><b>getProperty</b></code>(String key,<br> &#32; &#32; &#32;String defaultValue)</code></font>
<p>
<a name="23087"></a>
If there is an entry in this <code>Properties</code> table with <code>key</code> as its key, the associated 
element is returned. Otherwise, if this <code>Properties</code> table has a defaults table, then 
whatever its <code>getProperty</code> method returns is returned. Otherwise, <code>defaultValue</code> 
is returned.
<p><a name="23088"></a>
<p><font size=+1><strong>21.6.6   </strong> <code>public Enumeration <code><b>propertyNames</b></code>()</code></font>
<p>
<a name="23092"></a>
An <code>Enumeration</code> <a href="javautil.doc.html#23147">(&#167;21.1)</a> is returned that will generate all the keys for which this 
<code>Properties</code> table could supply an associated element. If this <code>Properties</code> table 
has a defaults table <a href="javautil.doc5.html#23078">(&#167;21.6.1)</a>, then keys for which the defaults table has entries are 
also supplied by the <code>Enumeration</code>, and so on, recursively; but no key is supplied 
by the <code>Enumeration</code> more than once.
<p><a name="23097"></a>
<p><font size=+1><strong>21.6.7   </strong> <code>public void <code><b>load</b></code>(InputStream in) throws IOException</code></font>
<p>
<a name="23098"></a>
Properties (key and element pairs) are read from the input stream:
<p><pre><a name="23762"></a>Runtime.getRuntime().getLocalizedInputStream(in)
</pre><a name="23760"></a>
and added to this <code>Properties</code> table. See the <code>getLocalizedInputStream</code> 
method of <code>Runtime</code> <a href="javalang.doc15.html#52515">(&#167;20.16.15)</a>.
<p><a name="23104"></a>
Every property occupies one line of the input stream. Each line is terminated by a line terminator (<code>\n</code> or <code>\r</code> or <code>\r\n</code>). Lines from the input stream are processed until end of file is reached on the input stream.<p>
<a name="23105"></a>
A line that contains only whitespace <a href="javalang.doc10.html#36320">(&#167;20.5.19)</a> or whose first non-whitespace character is an ASCII <code>#</code> or <code>!</code> is ignored (thus, <code>#</code> or <code>!</code> indicate comment lines).<p>
<a name="23106"></a>
Every line other than a blank line or a comment line describes one property to be added to the table (except that if a line ends with \, then the following line is treated as a continuation line, as described below). The key consists of all the characters in the line starting with the first non-whitespace character and up to, but not including, the first ASCII <code>=</code>, <code>:</code>, or whitespace character. Any whitespace after the key is skipped; if the first non-whitespace character after the key is <code>=</code> or <code>:</code>, then it is ignored and any whitespace characters after it are also skipped. All remaining characters on the line become part of the associated element string. Within the element string (but not the key), the ASCII escape sequences <code>\t</code>, <code>\n</code>, <code>\r</code>, <code>\\</code>, <code>\"</code>, <code>\'</code>, <code>\ &#32;</code> &#32;(a backslash and a space), and <code>\u</code><i>xxxx</i> are recognized and converted to single characters. Moreover, if the last character on the line is <code>\</code>, then the next line is treated as a continuation of the current line; the <code>\</code> and line terminator are simply discarded, and any leading whitespace characters on the continuation line are also discarded and are not part of the element string.<p>
<a name="23107"></a>
As an example, each of the following four lines specifies the key <code>"Truth"</code> and the associated element value <code>"Beauty"</code>:<p>
<pre><a name="23108"></a>
Truth Beauty
<a name="23109"></a>Truth = Beauty
<a name="23110"></a>	Truth:Beauty
<a name="23111"></a>Truth			:Beauty
</pre><a name="23112"></a>
As another example, the following three lines specify a single property:<p>
<pre><a name="23113"></a>
fruits				apple, banana, pear, \
<a name="23114"></a>				cantaloupe, watermelon, \
<a name="23115"></a>				kiwi, mango
</pre><a name="23116"></a>
The key is <code>"fruit"</code> and the associated element is:<p>
<pre><a name="23117"></a>"apple, banana, pear, cantaloupe, watermelon, kiwi, mango" 
</pre><a name="23780"></a>
Note that a space appears before each <code>\</code> so that a space will appear after each comma in the final result; the <code>\</code>, line terminator, and leading whitespace on the continuation line are merely discarded and are <i>not</i> replaced by one or more other characters.<p>
<a name="23776"></a>
As a third example, the line:<p>
<pre><a name="23119"></a>cheeses
</pre><a name="23120"></a>
specifies that the key is <code>"cheeses"</code> and the associated element is the empty string.<p>
<a name="23121"></a>
<p><font size=+1><strong>21.6.8   </strong> <code>public void <code><b>save</b></code>(OutputStream out, String header)</code></font>
<p>
<a name="23122"></a>
All the properties (key and element pairs) in this <code>Properties</code> table are written to 
the output stream:
<p><pre><a name="23792"></a>Runtime.getRuntime().getLocalizedOutputStream(out)
</pre><a name="23790"></a>
in a format suitable for loading into a <code>Properties</code> table using the <code>load</code> method 
<a href="javautil.doc5.html#23097">(&#167;21.6.7)</a>. See the <code>getLocalizedOutputStream</code> method of <code>Runtime</code> 
<a href="javalang.doc15.html#14097">(&#167;20.16.16)</a>.
<p><a name="23131"></a>
Properties from the defaults table of this <code>Properties</code> table (if any) are <i>not</i> written out by this method.<p>
<a name="23132"></a>
If the header argument is not null, then an ASCII <code>#</code> character, the header string, and a newline are first written to the output stream. Thus, the <code>header</code> can serve as an identifying comment.<p>
<a name="23133"></a>
Next, a comment line is always written, consisting of an ASCII <code>#</code> character, the current date and time (as if produced by the <code>toString</code> method of <code>Date</code> <a href="javautil.doc2.html#8902">(&#167;21.3.7)</a> for the current time), and a newline.<p>
<a name="23137"></a>
Then every entry in this <code>Properties</code> table is written out, one per line. For each entry the key string is written, then an ASCII <code>=</code>, then the associated element string. Each character of the element string is examined to see whether it should be rendered as an escape sequence. The ASCII characters <code>\</code>, tab, newline, and carriage return are written as <code>\\</code>, <code>\t</code>, <code>\n</code>, and <code>\r</code>, respectively. Characters less than <code>\u0020</code> and characters greater than <code>\u007E</code> (if necessary, depending on the needs of the localized output stream) are written as <code>\u</code><i>xxxx</i> for the appropriate hexadecimal value <i>xxxx</i>. Leading space characters, but not embedded or trailing space characters, are written with a preceding <code>\</code>.<p>
<a name="23138"></a>
<p><font size=+1><strong>21.6.9   </strong> <code>public void <code><b>list</b></code>(PrintStream out)</code></font>
<p>
<a name="23139"></a>
Properties (key and element pairs) in this <code>Properties</code> table are written to the output
stream <code>out</code> in a possibly abbreviated form that may be more convenient for 
use in debugging than the output of the <code>save</code> method. No header is written, and 
element values longer than 40 character are truncated to the first 37 characters, to 
which the characters "<code>...</code>" are appended. Thus, if the names of the keys are not 
too long, there is a fighting chance that each property will fit into the space of one 
line of a physical output device.
<p>

<hr>
<!-- This inserts footnotes--><p>
<a href="index.html">Contents</a> | <a href="javautil.doc4.html">Prev</a> | <a href="javautil.doc6.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<p>
<font size=-1>Java Language Specification (HTML generated by Suzette Pelouch on February 24, 1998)<br>
<i><a href="jcopyright.doc.html">Copyright &#169 1996 Sun Microsystems, Inc.</a>
All rights reserved</i>
<br>
Please send any comments or corrections to <a href="mailto:doug.kramer@sun.com">doug.kramer@sun.com</a>
</font>
</body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -