溫馨提示×

Java中System.setProperty()用法

小云
133
2023-08-05 17:17:48
欄目: 編程語言

System.setProperty()方法用于設(shè)置Java系統(tǒng)屬性。

語法:

public static String setProperty(String key, String value)

參數(shù):

  • key:要設(shè)置的系統(tǒng)屬性的鍵。

  • value:要設(shè)置的系統(tǒng)屬性的值。

返回值:

  • 返回指定鍵的先前屬性(如果有)的字符串值,如果沒有鍵的先前屬性,則返回null。

示例:

public class Main {
public static void main(String[] args) {
// 設(shè)置系統(tǒng)屬性
System.setProperty("myKey", "myValue");
// 獲取系統(tǒng)屬性
String property = System.getProperty("myKey");
System.out.println(property); // 輸出:myValue
}
}

在示例中,首先使用System.setProperty()方法設(shè)置了一個名為"myKey"的系統(tǒng)屬性,值為"myValue"。然后使用System.getProperty()方法獲取了該系統(tǒng)屬性的值,并將其打印輸出。

0