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


Java KeyStore deleteEntry()用法及代碼示例


java.security.KeyStore類的deleteEntry(String alias)方法用於刪除請求的別名name的條目。

用法:

public final void deleteEntry(String alias)
  throws KeyStoreException

參數:此方法將別名的名稱作為要刪除其條目的String類型的參數來查找。


返回值:此方法無返回值。

異常:如果您不初始化此 key 庫,則此方法將引發KeyStoreException。

注意:由於不存在“ privatekey” key 庫,因此本文中的所有程序都無法在在線IDE上運行。您可以在係統上的Java編譯器上檢查此代碼。要檢查此代碼,請在係統上創建 key 庫“ privatekey”,並設置自己的 key 庫密碼以訪問該 key 庫。

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

示例1:

// Java program to demonstrate 
// getInstance() method 
  
import java.security.*; 
import java.security.cert.*; 
import java.util.*; 
import java.io.*; 
  
public class GFG { 
    public static void main(String[] argv) 
    { 
  
        try { 
  
            // creating Enumeration<String> object 
            // and initializing with null 
            Enumeration<String> e = null; 
  
            // creating the object of MessageDigest 
            // and getting instance 
            // By usng getInstance() method 
            KeyStore sr = KeyStore.getInstance("JKS"); 
  
            // keystore password is require to access keystore 
            char[] pass = ("123456").toCharArray(); 
  
            // creating and initializing 
            // object of InputStream 
            InputStream is 
                = new FileInputStream( 
                    "f:/java/private key.store"); 
  
            // initializing keystore object 
            sr.load(is, pass); 
  
            // getting the list of aliases 
            // using aliases() method 
            e = sr.aliases(); 
  
            // display the result 
            System.out.println("List of all the alias"
                               + " present before deletion"); 
            while (e.hasMoreElements()) { 
                System.out.print(e.nextElement() + " "); 
                System.out.println(); 
            } 
  
            // delete the entry having alias name htttp 
            // using deleteEntry method 
            sr.deleteEntry("htttp"); 
  
            // getting the list of aliases 
            // using aliases() method 
            e = sr.aliases(); 
  
            // display the result 
            System.out.println("\nList of all the alias"
                               + " present after deletion"); 
            while (e.hasMoreElements()) { 
                System.out.print(e.nextElement() + " "); 
                System.out.println(); 
            } 
        } 
  
        catch (NoSuchAlgorithmException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
        catch (NullPointerException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
        catch (KeyStoreException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
        catch (FileNotFoundException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
        catch (IOException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
        catch (CertificateException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
    } 
}
輸出:
List of all the alias present before deletion
ftpkey
htttp

List of all the alias present after deletion
ftpkey

示例2:對於

// Java program to demonstrate 
// getInstance() method 
  
import java.security.*; 
import java.security.cert.*; 
import java.util.*; 
import java.io.*; 
  
public class GFG { 
    public static void main(String[] argv) 
    { 
  
        try { 
  
            // creating Enumeration<String> object 
            // and initializing with null 
            Enumeration<String> e = null; 
  
            // creating the object of MessageDigest 
            // and getting instance 
            // By usng getInstance() method 
            KeyStore sr = KeyStore.getInstance("JKS"); 
  
            // initializing keystore object 
            // sr.load(is, pass); 
  
            // getting the list of aliases 
            // using aliases() method 
            e = sr.aliases(); 
  
            // display the result 
            System.out.println("List of all the alias "
                               + "present before deletion"); 
            while (e.hasMoreElements()) { 
                System.out.print(e.nextElement() + " "); 
                System.out.println(); 
            } 
  
            // delete the entry having alias name htttp 
            // using deleteEntry method 
            sr.deleteEntry("htttp"); 
  
            // getting the list of aliases 
            // using aliases() method 
            e = sr.aliases(); 
  
            // display the result 
            System.out.println("\nList of all the alias"
                               + " present after deletion"); 
            while (e.hasMoreElements()) { 
                System.out.print(e.nextElement() + " "); 
                System.out.println(); 
            } 
        } 
        catch (NullPointerException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
        catch (KeyStoreException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
    } 
}
輸出:
Exception thrown :
 java.security.KeyStoreException:
 Uninitialized keystore

參考: https://docs.oracle.com/javase/9/docs/api/java/security/KeyStore.html#deleteEntry-java.lang.String-



相關用法


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