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
广告
© . All rights reserved.