Java System类clearProperty()方法及示例
系统类clearProperty()方法
clearProperty()方法在java.lang包中可用。
clearProperty()方法用于删除或清除由给定参数(property_name)表示的属性值。
clearProperty()方法是静态的,因此也可以使用类名访问此方法。
clearProperty()方法可能会引发各种类型的异常,该异常如下所示:
SecurityException:如果特定方法checkPropertyAcess()不允许访问该方法中的给定系统属性。
NullPointerException:如果给定的系统属性为null。
IllegalArgumentException:如果给定的系统属性值为空。
语法:
public static String clearProperty(String property);
参数:
property–表示要删除的属性的名称。
返回值:
此方法的返回类型为String,它返回System属性的旧字符串值,如果没有带有“property_name”的属性值,则返回null。
示例
//Java程序演示的例子
//clearProperty()系统类的方法。
public class ClearPropertyMethod {
public static void main(String[] args) {
//通过使用getProperty()方法
//获取属性的值
System.out.println(System.getProperty("user.name"));
//通过使用clearProperty()方法用于
//清除属性的值
System.clearProperty("user.name");
//显示属性的值
System.out.println("After clearProperty()...");
System.out.println(System.getProperty("user.name"));
}
}输出结果
E:\Programs>javac ClearPropertyMethod.java E:\Programs>java ClearPropertyMethod root After clearProperty()... null