欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

Java准确的获取操作系统的名称,java获取操作系统,在我们日常开发中,经常需

来源: javaer 分享于  点击 45678 次 点评:50

Java准确的获取操作系统的名称,java获取操作系统,在我们日常开发中,经常需


在我们日常开发中,经常需要判断操作系统的版本或者系统的名字等等。这就需要我们用到jdk默认带的一些属性了。这里我对各个版本的系统都做了区分,分别能判断mac,linux,window等大众的操作系统名称。直接看代码(OSUtil.java):```java package com.herman.util;
/* * @see 获取操作系统名称 * @author Herman.Xiong * @date 2014年4月22日 13:36:14 * @version V1.0 * @since jdk 1.6 /
public class OSUtil {
private static final boolean osIsMacOsX;
private static final boolean osIsWindows;
private static final boolean osIsWindowsXP;
private static final boolean osIsWindows2003;
private static final boolean osIsWindowsVista;
private static final boolean osIsLinux;
private static final boolean osIsWindowsWin7;
private static final boolean osIsWindowsWin8;

    static {          String os = System.getProperty("os.name");          if (os != null)              os = os.toLowerCase();          osIsMacOsX = "mac os x".equals(os);          osIsWindows = os != null && os.indexOf("windows") != -1;          osIsWindowsXP = "windows xp".equals(os);          osIsWindows2003 = "windows 2003".equals(os);          osIsWindowsVista = "windows vista".equals(os);          osIsLinux = os != null && os.indexOf("linux") != -1;          osIsWindowsWin7 = os !=null && os.indexOf("windows 7") != -1;          osIsWindowsWin8 = os !=null && os.indexOf("windows 8") != -1;      }    public static boolean isMacOSX() {          return osIsMacOsX;      }    public static boolean isWindows() {          return osIsWindows;      }    public static boolean isWindowsXP() {          return osIsWindowsXP;      }    public static boolean isWindows2003() {          return osIsWindows2003;      }    public static boolean isWindowsVista() {          return osIsWindowsVista;      }    public static boolean isLinux() {          return osIsLinux;      }    public static boolean IsWindowsWin7(){          return osIsWindowsWin7;      }    public static boolean IsWindowsWin8(){          return osIsWindowsWin8;      }  }
精华都在上面。下面我们用各种jdk版本和各种操作系统进行测试。测试代码(TestOS.java):```java    package com.herman.test;      import com.herman.util.OSUtil;      /**      * @see 测试操作系统类型      * @author Herman.Xiong      * @date 2014年4月22日 13:40:06      * @version V1.0      * @since jdk 1.6      */      public class TestOs {          public static String getVersion(){              return "当前版本 V1.0 2014年4月22日 11:57:20";          }           public static void main(String[] args) {              System.out.println(System.getProperty("os.name"));              System.out.println(getVersion());              System.out.println("当前系统为Mac系统:"+OSUtil.isMacOSX());              System.out.println("当前系统为Linux系统:"+OSUtil.isLinux());              System.out.println("当前系统为Window系统:"+OSUtil.isWindows());              System.out.println("当前系统为Windows2003系统:"+OSUtil.isWindows2003());              System.out.println("当前系统为WindowsVista系统:"+OSUtil.isWindowsVista());              System.out.println("当前系统为WindowsXP系统:"+OSUtil.isWindowsXP());              /*              * 注意:如果使用的1.6的jdk,那么对于win7和win8系统都显示的“Windows Vista”              * 如果要正确判断win7还是win8系统,则需要使用jdk1.7              * android系统需要稍微改动              */              System.out.println("当前系统为Windows7系统:"+OSUtil.IsWindowsWin7());              System.out.println("当前系统为Windows8系统:"+OSUtil.IsWindowsWin8());          }      }  
相关栏目:

用户点评