当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。