Documentation

The Java™ Tutorials
Hide TOC
Properties属性
Trail: Essential Java Classes
Lesson: The Platform Environment
Section: Configuration Utilities

Properties性质

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:此类提供了以下方面的方法:

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.HashtableSome of the methods inherited from Hashtable support the following actions:Hashtable继承的某些方法支持以下操作:


Security Considerations:安全考虑: Access to properties is subject to approval by the current security manager. 对财产的访问须经现任安全经理批准。The example code segments in this section are assumed to be in standalone applications, which, by default, have no security manager. 本节中的示例代码段假定位于独立应用程序中,默认情况下,这些应用程序没有安全管理器。The same code in an applet may not work depending on the browser in which it is running. 小程序中的相同代码可能无法工作,具体取决于运行它的浏览器。See What Applets Can and Cannot Do in the Java Applets lesson for information about security restrictions on applets. 有关小程序安全限制的信息,请参阅Java小程序课程中的小程序可以做什么和不能做什么

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.本节的其余部分解释如何使用属性管理应用程序配置。

Properties in the Application Life Cycle应用程序生命周期中的属性

The following figure illustrates how a typical application might manage its configuration data with a Properties object over the course of its execution.下图说明了典型应用程序在执行过程中如何使用Properties对象管理其配置数据。

Possible lifecycle of a Properties object

Setting Up the Properties Object设置属性对象

The 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.此文件中的属性是上次调用应用程序时从应用程序保存的属性,如下一节所述。

Saving Properties保存属性

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方法需要一个要写入的流,以及在输出顶部用作注释的字符串。

Getting Property Information获取Property信息

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类有几种获取属性信息的方法:

Setting Properties设置属性

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对象中的属性:


Note: Some of the methods described above are defined in Hashtable, and thus accept key and value argument types other than String. 上面描述的一些方法是在Hashtable中定义的,因此接受除String以外的键和值参数类型。Always use Strings for keys and values, even if the method allows other types. 始终使用String作为键和值,即使该方法允许其他类型。Also do not invoke Hashtable.set or Hastable.setAll on Properties objects; always use Properties.setProperty. 也不要在属性对象上调用Hashtable.setHastable.setAll;始终使用Properties.setProperty

Previous page: Configuration Utilities
Next page: Command-Line Arguments