Java System类setProperty()方法及示例

系统类setProperty()方法 (System class setProperty() method)

  • setProperty() method is available in java.lang package.

    setProperty()方法在java.lang包中可用。

  • setProperty() method is used to sets the system property denoted by the given parameter (system_property) with the given another parameter (system_property_value).

    setProperty()方法用于将给定参数( system_property )表示的系统属性与给定另一个参数( system_property_value )一起设置。

  • setProperty() method is a static method, so it is accessible with the class name too.

    setProperty()方法是静态方法,因此也可以使用类名进行访问。

  • setProperty() method method throws various exceptions at the time of setting the system property

    setProperty()方法方法在设置系统属性时会引发各种异常

    • SecurityException: In this exception, its checkPermission() method can't allow access to the given system properties when the security manager exists.SecurityException :在此异常中,当安全管理器存在时,其checkPermission()方法不允许访问给定的系统属性。
    • NullPointerException: In this exception, if the given system_property or given system_property_value is null.
    • NullPointerException :在此异常中,如果给定的system_property或给定的system_property_value为null。
    • IllegalArgumentException: In this exception, if the given system property is null.IllegalArgumentException :在此异常中,如果给定的系统属性为null。

Syntax:

句法:

    public static String setProperty(
            String  system_property, 
            String system_property_value);

Parameter(s):

参数:

  • ssystem_property – represents the name of the system property.

    ssystem_property –表示系统属性的名称。

  • ssystem_property_value – represents the value of the system property.

    ssystem_property_value –表示系统属性的值。

Return value:

返回值:

The return type of this method is String, it returns the old value of the system property if exists else it returns null.

此方法的返回类型为String ,如果存在则返回系统属性的旧值,否则返回null。

Example:

例:

// Java program to demonstrate the example of 
// setProperty() method of System Class.

import java.lang.*;
import java.util.*;

public class SetPropertyMethod {
    public static void main(String[] args) {
        //Display previous operating system
        //architecture before setting properties
        System.out.print("Previous os name :" + " ");
        System.out.print(System.getProperty("os.name"));

        System.clearProperty("os.name");
        System.setProperty("os.name", "Ubuntu");

        System.out.println();

        //Display new operating system
        //architecture after setting properties
        System.out.print("New os name :" + " ");
        System.out.print(System.getProperty("os.name"));
    }
}

Output

输出量

E:\Programs>javac SetPropertyMethod.java
E:\Programs>java SetPropertyMethod
Previous os name : Linux
New os name : Ubuntu


翻译自: https://www.includehelp.com/java/system-class-setproperty-method-with-example.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Java中,可以使用`System.setProperty()`方法来设置系统级别的变量。如果您想要重置一个已经设置的系统变量,可以使用`System.clearProperty()`方法来清除该变量。 以下是将系统级别的变量重置为默认值的代码示例: ```java System.clearProperty("your.property.key"); //清除该变量 System.setProperty("your.property.key", "default value"); //重置为默认值 ``` 在上述代码示例中,您需要将"your.property.key"替换为要重置的系统变量的键,并将"default value"替换为该变量的默认值。 ### 回答2: Java中的System.setProperty()方法用于设置系统属性的值。这些系统属性通常用于在应用程序中共享全局变量或配置信息。 要将System.setProperty()方法设置的全局变量重置,可以使用System.clearProperty()方法。该方法允许我们移除之前设置的系统属性,并将其值重置为默认值。 例如,假设我们使用System.setProperty()方法将一个全局变量myVariable设置为"value1": System.setProperty("myVariable", "value1"); 然后,如果我们希望将该全局变量重置为默认值,可以使用System.clearProperty()方法System.clearProperty("myVariable"); 这样,系统将移除之前设置的myVariable属性,并将其重置为默认值。在这种情况下,myVariable属性将被删除,并且使用System.getProperty("myVariable")方法将返回null。 需要注意的是,System.clearProperty()方法只适用于通过System.setProperty()方法设置的全局变量。如果全局变量是通过其他方式设置的(例如通过命令行参数或配置文件),则无法使用该方法进行重置。 总结而言,要重置由System.setProperty()方法设置的全局变量,在Java中可以使用System.clearProperty()方法来移除该属性并将其重置为默认值。 ### 回答3: 在Java中,System.setProperty方法用于设置系统级属性,可以在程序中全局访问。 如果需要重置System.setProperty设置的全局变量,可以使用System.clearProperty方法来清除相应的系统属性。 该方法的语法如下: System.clearProperty(String key) 其中,key参数是要清除的系统属性的名称。 调用该方法后,系统属性将被重置为空值,无法再通过System.getProperty方法来获取相应的值。 例如,我们在程序中设置了一个全局的系统属性: System.setProperty("myProperty", "value"); 然后,如果我们想要重置该全局变量,可以调用以下代码: System.clearProperty("myProperty"); 该调用将清除名为"myProperty"的系统属性,使其不再可访问。 需要注意的是,System.clearProperty方法只能用于清除通过System.setProperty方法设置的全局变量,无法清除其他类型的全局变量或局部变量。此外,由于系统属性是全局的,一旦重置后,其他程序或线程将无法再访问该属性的值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值