您也可以使用.contains(String)方法,仅检查“ windows”字符串可能遵循
if (System.getProperty("os.name").toLowerCase().contains("windows") && System.getProperty("os.name").toLowerCase().contains(windows version here [xp, 7, 8, etc]))){}
如果您需要Windows版本,则可以检查所有版本,然后假定8.1或10可以解决该错误。
if (System.getProperty("os.name").toLowerCase().contains("windows") && System.getProperty("os.name").toLowerCase().contains("xp")){
//code for windows xp }
else if (System.getProperty("os.name").toLowerCase().contains("windows") && System.getProperty("os.name").toLowerCase().contains("vista")){
//code for windows vista
else if (System.getProperty("os.name").toLowerCase().contains("windows") && System.getProperty("os.name").toLowerCase().contains("7")){
//code for windows 7}
else if (System.getProperty("os.name").toLowerCase().contains("windows") && System.getProperty("os.name").toLowerCase().contains("8")){
//code for windows 8}
else if (System.getProperty("os.name").toLowerCase().contains("windows") && System.getProperty("os.name").toLowerCase().contains("8.1")){
//code for both windows 8.1 and 10
}
现在解释一下这里发生了什么:
if语句只是确定Windows版本的条件
.contains(String)以字符串形式返回os的名称
.contains(String)方法使返回的String小写
.contains(String)方法检查给定的输入字符串是否包含在正在调用的字符串中
最后一条语句允许每个操作系统使用不同的代码,但8.1和10除外,这需要作为一个块来处理:(