【问题标题】:Java Cross-Platform OS detection to declare file path locationJava 跨平台操作系统检测以声明文件路径位置
【发布时间】:2012-02-16 11:41:38
【问题描述】:

我目前包含以下无法编译的代码。 else if 语句报告“;' expected”。我不明白为什么我不能在这种情况下使用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;

【问题讨论】:

标签: java


【解决方案1】:

最好使用user.nameuser.home。您还可以使用file.separator 获取分隔符。 Check this out。这些属性将真正帮助您在不检查操作系统的情况下更干净地执行此操作。

那么还有一个问题是你需要改为使用else if,而不是elseif...

【讨论】:

    【解决方案2】:

    elseif 在 java 中不存在。您必须使用 else if 作为:

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

    【讨论】:

      【解决方案3】:

      java 中没有elseif 关键字。 你应该说else if(注意空格)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-05-18
        • 2014-05-05
        • 2015-03-21
        • 1970-01-01
        • 1970-01-01
        • 2010-09-12
        • 2014-06-09
        相关资源
        最近更新 更多