Java Properties stringPropertyNames() 方法



描述

java Properties stringPropertyNames() 方法返回此屬性列表中鍵和其對應值均為字串的鍵的集合,包括預設屬性列表中的不同鍵(如果在主屬性列表中尚未找到相同名稱的鍵)。鍵或值不是字串型別的屬性將被忽略。

宣告

以下是 java Properties stringPropertyNames() 方法的宣告

public Set<String> stringPropertyNames()

引數

返回值

此方法返回此屬性列表中鍵和其對應值均為字串的鍵的集合,包括預設屬性列表中的鍵。

異常

獲取屬性鍵名稱作為字串集合的示例

以下示例演示瞭如何使用 Java Properties stringPropertyNames() 方法列印 Properties 物件的屬性名稱。我們建立了一個 Properties 物件,然後添加了一些條目。使用 stringPropertyNames() 方法檢索並列印所有鍵。

package com.tutorialspoint;

import java.util.Properties;
import java.util.Set;

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

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

      // save the Property names in the set
      Set<String> set = prop.stringPropertyNames();

      // print the set
      System.out.println("" + set);
   }
}

輸出

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

[Width, Height]

更新後獲取屬性鍵名稱作為字串集合的示例

以下示例演示瞭如何使用 Java Properties stringPropertyNames() 方法列印 Properties 物件的屬性名稱。我們建立了一個 Properties 物件,然後添加了一些條目。使用 stringPropertyNames() 方法檢索並列印所有鍵。

package com.tutorialspoint;

import java.util.Properties;
import java.util.Set;

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

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

      // save the Property names in the set
      Set<String> set = prop.stringPropertyNames();

      // add a new property
      prop.put("Length", "150");
	  
      // get the updated set	  
      set = prop.stringPropertyNames();
	  
      // print the set again
      System.out.println("" + set);
   }
}

輸出

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

[Length, Height, Width]
java_util_properties.htm
廣告

© . All rights reserved.