Java Properties 屬性 propertyNames() 方法



描述

java Properties 屬性 propertyNames() 方法返回此屬性列表中所有鍵的列舉,包括預設屬性列表中的唯一鍵(如果在主屬性列表中尚未找到相同名稱的鍵)。

宣告

以下是 java.util.Properties.propertyNames() 方法的宣告

public Enumeration<?> propertyNames()

引數

返回值

此方法返回此屬性列表中所有鍵的列舉,包括預設屬性列表中的鍵。

異常

ClassCastException − 如果此屬性列表中的任何鍵不是字串。

從 Properties 示例獲取鍵的名稱

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

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());  
      }
   }
}

輸出

讓我們編譯並執行以上程式,這將產生以下結果:

Width
Height
java_util_properties.htm
廣告