Java 简明教程

Java Properties Class

Introduction

Java Properties 类表示一组持久性属性的类。Properties 既可以保存到流中,也可以从流中加载。以下是有关 Properties 的重要事项 −

The Java Properties class is a class which represents a persistent set of properties. The Properties can be saved to a stream or loaded from a stream. Following are the important points about Properties −

  1. Each key and its corresponding value in the property list is a string.

  2. A property list can contain another property list as its 'defaults', this second property list is searched if the property key is not found in the original property list.

  3. This class is thread-safe; multiple threads can share a single Properties object without the need for external synchronization.

Class declaration

以下是 java.util.Properties 类的声明 −

Following is the declaration for java.util.Properties class −

public class Properties
   extends Hashtable<Object,Object>

Field

以下是 java.util.Properties 类的字段 −

Following are the fields for java.util.Properties class −

protected Properties defaults − 这是包含此属性列表中找不到的任何键的默认值的属性列表。

protected Properties defaults − This is the property list that contains default values for any keys not found in this property list.

Class constructors

Class methods

Methods inherited

此类从以下类中继承方法:

This class inherits methods from the following classes −

  1. java.util.Hashtable

  2. java.util.Object

Getting an Enumeration of Properties Keys Example

以下示例展示了 java.util.Properties.propertyNames() 方法的用法。

The following example shows the usage of java.util.Properties.propertyNames() method.

package com.tutorialspoint;

import java.util.Enumeration;
import java.util.Properties;

public class PropertiesDemo {
   public static void main(String[] args) {
      Properties prop = new Properties();

      // add some properties
      prop.put("Height", "200");
      prop.put("Width", "15");

      // assign the property names in a enumaration
      Enumeration<?> enumeration = prop.propertyNames();

      // print the enumaration elements
      while(enumeration.hasMoreElements()) {
         System.out.println("" + enumeration.nextElement());
      }
   }
}

Output

让我们编译并运行上述程序,这将生成以下结果 −

Let us compile and run the above program, this will produce the following result −

Width
Height