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

你可能感兴趣的:(Java System类setProperty()方法及示例)