確定 Java 中的首選項節點是否存在


要確定 Java 中的首選項節點是否存在,我們使用 nodeExists() 方法。nodeExists() 方法返回一個布林值。當指定的首選項節點與此節點處於同一樹中時,它返回 true。

宣告 − java.util.prefs.Preferences.remove() 方法的宣告如下 −

public abstract boolean nodeExists(String pathname)throws BackingStoreException

其中 pathname 是需要確定其存在性的節點的路徑名。

下面我們來看看一個程式,用以確定 Java 中的首選項節點是否存在 −

示例

 線上演示

import java.util.prefs.Preferences;
public class Example {
   public static void main(String[] args) throws Exception {
      boolean exist = Preferences.userRoot().nodeExists("/node");
      System.out.println("Checking node existence before creation: "+exist);
      Preferences.userRoot().node("/node");
      exist = Preferences.userRoot().nodeExists("/node");
      System.out.println("Checking node existence after creation: "+exist);
   }
}

輸出

Checking node existence before creation: false
Checking node existence after creation: true
Dec 26, 2018 7:12:10 AM java.util.prefs.FileSystemPreferences$1 run
INFO: Created user preferences directory.

更新於: 2020-06-25

571 次瀏覽

開啟你的事業

完成課程,獲得認證

開始
廣告
© . All rights reserved.