Java System.setProperty()怎么使用

小億
119
2023-08-18 05:54:30
欄目: 編程語言

Java中的System.setProperty()方法用于設(shè)置系統(tǒng)屬性。它的語法如下:

public static String setProperty(String key, String value)

其中,key是要設(shè)置的屬性的鍵,value是要設(shè)置的屬性的值。

下面是一個(gè)示例:

public class Main {
public static void main(String[] args) {
// 設(shè)置系統(tǒng)屬性java.version的值為1.8
System.setProperty("java.version", "1.8");
// 獲取并打印java.version的值
String javaVersion = System.getProperty("java.version");
System.out.println("Java Version: " + javaVersion);
}
}

輸出:

Java Version: 1.8

上述示例中,我們首先使用System.setProperty()方法設(shè)置了系統(tǒng)屬性java.version的值為1.8,然后使用System.getProperty()方法獲取并打印了java.version的值。

0