简体   繁体   English

用于声明文件路径位置的Java跨平台操作系统检测

[英]Java Cross-Platform OS detection to declare file path location

I currently comprise the following code which fails to compile. 我目前包含以下无法编译的代码。 The else if statement reports that ' ;' expected else if语句报告' ;' expected ;' expected . ;' expected I don't understand why I can't use a else if for this scenario? 我不明白为什么我不能使用else if在这种情况下?

public class FileConfiguration {

    private String checkOs() {
        String path = "";        
        if (System.getProperty("os.name").startsWith("Windows")) {
            // includes: Windows 2000,  Windows 95, Windows 98, Windows NT, Windows Vista, Windows XP
            path = "C://Users//...";            

        }
        elseif (System.getProperty("os.name").startsWith("Mac")) {
            path = "///Users//...";
        }
        return path;        

    }

    // declare paths for file source and destination 
    String destinationPath = path;
    String sourcePath = path;

It would be better if you were to use user.name and user.home . 如果你使用user.nameuser.home会更好。 You can also get the separator using file.separator . 您还可以使用file.separator获取分隔符。 Check this out . 看看这个 Those properties will really help you do this more cleanly without checking the OS. 这些属性将真正帮助您在不检查操作系统的情况下更干净地完成此操作。

Then there's also the matter of you needing to change to using else if , not elseif ... 再有就是还的,你需要改变使用此事else if不是elseif ...

elseif does not exist in java. elseif在java中不存在。 You must use else if as: 你必须使用else if

if (a) {
// code
} else if (b) {
// code
}

There is no elseif keyword in java. java中没有elseif关键字。 You should say else if (pay attention on the space) else if (注意空间)你应该说

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM