當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Provider getProperty()用法及代碼示例


java.security.Provider類的getProperty()方法用於在此屬性列表中搜索具有指定鍵的屬性。如果在此屬性列表中找不到該鍵,則將遞歸檢查默認屬性列表及其默認值。如果找不到該屬性,則該方法返回null。

用法:

public String getProperty(String key)

參數:此方法以屬性鍵為參數。


返回值:此方法返回具有指定鍵值的此屬性列表中的值;如果找不到該屬性,則返回null。

下麵是說明getProperty()方法的示例:

示例1:

// Java program to demonstrate 
// getProperty() method 
  
import java.security.*; 
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) throws Exception 
    { 
        // Declaring int values 
        int i = 10; 
  
        try { 
            // creating the object of  KeyPairGenerator 
            KeyPairGenerator sr = KeyPairGenerator.getInstance("DSA", "SUN"); 
  
            // getting the Provider of the KeyPairGenerator sr 
            // by using method getProvider() 
            Provider provider = sr.getProvider(); 
  
            // Declaring the variable of set<Map> type 
            Set<Object> set; 
  
            // getting unmodifiable Set view of the property entries 
            set = provider.keySet(); 
  
            // Creating the object of iterator to iterate set 
            Iterator iter = set.iterator(); 
  
            while (i > 0) { 
  
                // getting the mapped value in element 
                // using getProperty() method 
                String property = provider.getProperty((String)iter.next()); 
  
                // printing the property of specified key 
                System.out.println("value is : " + property); 
                i--; 
            } 
        } 
  
        catch (NoSuchAlgorithmException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
    } 
}
輸出:
value is : SHA1withDSA
value is : SHA1withDSA
value is : SHA1withDSA
value is : Software
value is : sun.security.provider.JavaKeyStore$DualFormatJKS
value is : SHA
value is : sun.security.provider.SHA
value is : sun.security.provider.JavaKeyStore$CaseExactJKS
value is : Software
value is : sun.security.provider.DSA$SHA256withDSA

示例2:

// Java program to demonstrate 
// getProperty() method 
  
import java.security.*; 
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) throws Exception 
    { 
        // Declaring int values 
        int i = 10; 
  
        try { 
            // creating the object of  KeyPairGenerator 
            KeyPairGenerator sr = KeyPairGenerator.getInstance("DSA", "SUN"); 
  
            // getting the Provider of the KeyPairGenerator sr 
            // by using method getProvider() 
            Provider provider = sr.getProvider(); 
  
            // getting the mapped value in element 
            // using getProperty() method 
            System.out.println("Trying to search for unspecified key"); 
            String property = provider.getProperty("geeks"); 
  
            // printing the property of specified key 
            System.out.println("value is : " + property); 
        } 
  
        catch (NoSuchAlgorithmException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
    } 
}
輸出:
Trying to search for unspecified key
value is : null


相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 Provider getProperty() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。