1.原理
非常简单,调用System.getProperty()方法,参数传入"os.name"即可。
2.代码
public static void main(String[] args) {
String osName = System.getProperty("os.name");
System.out.println(osName);
if (osName.startsWith("Mac OS")) {
// 苹果
} else if (osName.startsWith("Windows")) {
// windows
} else {
// unix or linux
}
}
3.效果图

4.总结
System.getProperty(“os.name”)是完全可以判断出操作系统是Windows还是Linux亦或者是Mac os。
本文介绍了一种使用Java代码判断当前运行的操作系统类型的方法。通过调用System.getProperty()方法并传入os.name参数,可以获取操作系统的名称,进而判断其为Windows、Linux还是Mac OS。文中提供了一个示例代码,展示了如何实现这一功能。
1046

被折叠的 条评论
为什么被折叠?



