在此示例中,我們將學(xué)習(xí)使用Java程序獲取當(dāng)前操作系統(tǒng)的名稱和版本。
class Main { public static void main(String[] args) { //獲取操作系統(tǒng)的名稱 String operatingSystem = System.getProperty("os.name"); System.out.println(operatingSystem); } }
輸出結(jié)果
Windows 10
在上面的示例中,我們使用了System類的 getProperty() 方法。在這里,我們將 os.name 鍵作為參數(shù)傳遞給該方法。
該方法返回由鍵指定的系統(tǒng)的屬性。
還有其他鍵可用于獲取其他系統(tǒng)屬性。例如,
//返回操作系統(tǒng)的版本 // 10.0 System.getProperty("os.version");
在此,鍵 os.version 返回操作系統(tǒng)的版本。