Java System clearProperty() 方法



描述

Java System clearProperty() 方法移除由指定鍵指示的系統屬性。

宣告

以下是 java.lang.System.clearProperty() 方法的宣告

public static String clearProperty(String key)

引數

key − 要移除的系統屬性的名稱。

返回值

此方法返回系統屬性之前的字串值,如果該鍵沒有屬性,則返回 null。

異常

  • SecurityException − 如果存在安全管理器且其 checkPropertyAccess 方法不允許訪問指定的系統屬性。

  • NullPointerException − 如果 key 為 null。

  • IllegalArgumentException − 如果 key 為空。

示例:清除使用者目錄並設定新路徑

以下示例演示了 Java System clearProperty() 方法的用法。在這個示例中,我們使用 System.clearProperty() 方法清除了當前的 user.dir 系統屬性。我們使用了 clearProperty() 的返回值來列印之前的 user.dir 屬性值。使用 setProperty() 方法,將新路徑設定為 user.dir,並使用 getProperty() 獲取並列印此更新後的屬性。

package com.tutorialspoint;

public class SystemDemo {

   public static void main(String[] args) {

      // returns the previous string value of the system property
      String userDir = System.clearProperty("user.dir");
      System.out.println("Previous user.dir = " + userDir);

      // sets the new property
      System.setProperty("user.dir", "C:/tutorialspoint/java");

      // gets the system property after changes done by setProperty
      System.out.println("Current user.dir = " + System.getProperty("user.dir"));
   }
} 

輸出

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

Previous user.dir = C:\Users\Tutorialspoint\eclipse-workspace\Tutorialspoint
Current user.dir = C:/tutorialspoint/java

示例:清除 Java 類路徑並設定新路徑

以下示例演示了 Java System clearProperty() 方法的用法。在這個示例中,我們使用 System.clearProperty() 方法清除了當前的 java.class.path 系統屬性。我們使用了 clearProperty() 的返回值來列印之前的 java.class.path 屬性值。使用 setProperty() 方法,將新路徑設定為 java.class.path,並使用 getProperty() 獲取並列印此更新後的屬性。

package com.tutorialspoint;

public class SystemDemo {

   public static void main(String[] args) {

      // returns the previous string value of the system property
      String classPath = System.clearProperty("java.class.path");
      System.out.println("Previous Class Path = " + classPath);

      // sets the new property
      System.setProperty("java.class.path", "C:/tutorialspoint/java");

      // gets the system property after changes done by setProperty
      System.out.println("Current Class Path = " + System.getProperty("java.class.path"));
   }
} 

輸出

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

Previous Class Path = C:\Users\Tutorialspoint\eclipse-workspace\Tutorialspoint\bin;C:\Users\Tutorialspoint\eclipse-workspace\Tutorialspoint\lib\jmh-core-1.37.jar;C:\Users\Tutorialspoint\eclipse-workspace\Tutorialspoint\lib\jmh-generator-annprocess-1.37.jar;C:\Users\Tutorialspoint\eclipse-workspace\Tutorialspoint\lib
Current Class Path = C:/tutorialspoint/java

示例:清除不存在的屬性並設定新值

以下示例演示了 Java System clearProperty() 方法的用法。在這個示例中,我們使用 System.clearProperty() 方法清除了當前的 tmp.dir 系統屬性。我們使用了 clearProperty() 的返回值來列印之前的 tmp.dir 屬性值(為 null)。使用 setProperty() 方法,將新路徑設定為 tmp.dir,並使用 getProperty() 獲取並列印此更新後的屬性。

package com.tutorialspoint;

public class SystemDemo {

   public static void main(String[] args) {

      // returns the previous string value of the system property
      String tmpDir = System.clearProperty("tmp.dir");
      System.out.println("Previous Temporary Dir = " + tmpDir);

      // sets the new property
      System.setProperty("tmp.dir", "C:/tutorialspoint/java");

      // gets the system property after changes done by setProperty
      System.out.println("Current Temporary Dir = " + System.getProperty("tmp.dir"));
   }
}  

輸出

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

Previous Temporary Dir = null
Current Temporary Dir = C:/tutorialspoint/java
java_lang_system.htm
廣告