The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
Properties are configuration values managed as key/value pairs. 属性是作为键/值对管理的配置值。In each pair, the key and value are both 在每对中,键和值都是String values. String值。The key identifies, and is used to retrieve, the value, much as a variable name is used to retrieve the variable's value. 键标识并用于检索值,就像变量名用于检索变量的值一样。For example, an application capable of downloading files might use a property named "download.lastDirectory" to keep track of the directory used for the last download.例如,能够下载文件的应用程序可能会使用名为“download.lastDirectory”的属性来跟踪上次下载使用的目录。
To manage properties, create instances of 要管理属性,请创建java.util.Properties. java.util.Properties的实例。This class provides methods for the following:此类提供了以下方面的方法:
Properties object from a stream,Properties对象中,For an introduction to streams, refer to the section I/O Streams in the Basic I/O lesson.有关流的介绍,请参阅基本I/O课程中的I/O流一节。
Properties extends java.util.Hashtable. Properties扩展了java.util.Hashtable。Some of the methods inherited from 从Hashtable support the following actions:Hashtable继承的某些方法支持以下操作:
Properties object,Properties对象中是否存在特定的键或值,Properties list,Properties列表中,Properties object is empty.Properties对象是否为空。The System class maintains a Properties object that defines the configuration of the current working environment. System类维护一个Properties对象,该对象定义当前工作环境的配置。For more about these properties, see System Properties. 有关这些属性的详细信息,请参阅系统属性。The remainder of this section explains how to use properties to manage application configuration.本节的其余部分解释如何使用属性管理应用程序配置。
The following figure illustrates how a typical application might manage its configuration data with a 下图说明了典型应用程序在执行过程中如何使用Properties object over the course of its execution.Properties对象管理其配置数据。

Starting UpProperties object. Properties对象中。.class and other resource files for the application..class和其他资源文件一起存储在磁盘上的文件中。Properties object and loads the properties that were saved from the last time the application was run. Properties对象,并加载上次运行应用程序时保存的属性。RunningProperties object is updated to reflect these changes. Properties对象会更新以反映这些更改。ExitingThe following Java code performs the first two steps described in the previous section: loading the default properties and loading the remembered properties:以下Java代码执行上一节中描述的前两个步骤:加载默认属性和加载记住的属性:
. . .
// create and load default properties
Properties defaultProps = new Properties();
FileInputStream in = new FileInputStream("defaultProperties");
defaultProps.load(in);
in.close();
// create application properties with default
Properties applicationProps = new Properties(defaultProps);
// now load properties
// from last invocation
in = new FileInputStream("appProperties");
applicationProps.load(in);
in.close();
. . .First, the application sets up a default 首先,应用程序设置一个默认Properties object. Properties对象。This object contains the set of properties to use if values are not explicitly set elsewhere. 此对象包含在其他位置未显式设置值时要使用的属性集。Then the load method reads the default values from a file on disk named 然后加载方法从磁盘上名为defaultProperties.defaultProperties的文件中读取默认值。
Next, the application uses a different constructor to create a second 接下来,应用程序使用不同的构造函数创建第二个Properties object, applicationProps, whose default values are contained in defaultProps. Properties对象applicationProps,其默认值包含在defaultProps中。The defaults come into play when a property is being retrieved. 当检索属性时,默认值起作用。If the property can't be found in 如果在applicationProps, then its default list is searched.applicationProps中找不到该属性,则搜索其默认列表。
Finally, the code loads a set of properties into 最后,代码从名为applicationProps from a file named appProperties. appProperties的文件将一组属性加载到applicationProps中。The properties in this file are those that were saved from the application the last time it was invoked, as explained in the next section.此文件中的属性是上次调用应用程序时从应用程序保存的属性,如下一节所述。
The following example writes out the application properties from the previous example using 以下示例使用Properties.store. Properties.store写出上一示例中的应用程序属性。The default properties don't need to be saved each time because they never change.默认属性不需要每次都保存,因为它们从不更改。
FileOutputStream out = new FileOutputStream("appProperties");
applicationProps.store(out, "---No Comment---");
out.close();The store method needs a stream to write to, as well as a string that it uses as a comment at the top of the output.store方法需要一个要写入的流,以及在输出顶部用作注释的字符串。
Once the application has set up its 一旦应用程序设置了其Properties object, the application can query the object for information about various keys and values that it contains. Properties对象,应用程序就可以查询该对象,以获取有关其包含的各种键和值的信息。An application gets information from a 应用程序在启动后从Properties object after start up so that it can initialize itself based on choices made by the user. Properties对象获取信息,以便根据用户所做的选择对自身进行初始化。The Properties class has several methods for getting property information:Properties类有几种获取属性信息的方法:
contains(Object value)containsKey(Object key)true if the value or the key is in the Properties object. Properties对象中,则返回true。Properties inherits these methods from Hashtable. Properties从Hashtable继承这些方法。Object arguments, but only String values should be used.Object参数,但只应使用String值。getProperty(String key)getProperty(String key, String default)list(PrintStream s)list(PrintWriter w)elements()keys()propertyNames()Enumeration containing the keys or values (as indicated by the method name) contained in the Properties object. Properties对象中包含的键或值(由方法名称指示)的Enumeration。keys method only returns the keys for the object itself; the propertyNames method returns the keys for default properties as well.keys方法只返回对象本身的键;propertyNames方法还返回默认属性的键。stringPropertyNames()propertyNames, but returns a Set<String>, and only returns names of properties where both key and value are strings. propertyNames类似,但返回一个Set<String>,并且只返回键和值都是字符串的属性的名称。Set object is not backed by the Properties object, so changes in one do not affect the other.Set对象不受Properties对象的支持,因此其中一个对象中的更改不会影响另一个对象。size()A user's interaction with an application during its execution may impact property settings. 用户在应用程序执行期间与应用程序的交互可能会影响属性设置。These changes should be reflected in the 这些更改应反映在Properties object so that they are saved when the application exits (and calls the store method). Properties对象中,以便在应用程序退出(并调用store方法)时保存这些更改。The following methods change the properties in a 以下方法更改Properties object:Properties对象中的属性:
setProperty(String key, String value)Properties object.Properties对象中。remove(Object key)Hashtable, and thus accept key and value argument types other than String. Hashtable中定义的,因此接受除String以外的键和值参数类型。Strings for keys and values, even if the method allows other types. String作为键和值,即使该方法允许其他类型。Hashtable.set or Hastable.setAll on Properties objects; always use Properties.setProperty. Hashtable.set或Hastable.setAll;始终使用Properties.setProperty。