判断当前系统是Linux还是Windows

package com.shucha.deveiface.biz.utils;

import com.sdy.common.utils.StringUtil;

/**
 * @author tqf
 * @Description 判断当前系统类型 Linux还是Windows
 * @Version 1.0
 * @since 2022-04-12 11:34
 */
public class OsPathUtil {
    public static String convertToUnix(String path) {
        if (StringUtil.isBlank(path)) {
            return path;
        }
        if (path.startsWith("e:/") || path.startsWith("E:/")) {
            path = path.substring(2);
        }
        return path;
    }

    public static String convertToWindows(String path) {
        if (StringUtil.isBlank(path)) {
            return path;
        }
        if (!(path.startsWith("e:/") || path.startsWith("E:/"))) {
            path = "E:".concat(path);
        }
        return path;
    }

    public static String convertWithOs(String path) {
        if (StringUtil.isBlank(path)) {
            return path;
        }
        String os = System.getProperty("os.name");
        if (os.toLowerCase().startsWith("win")) {
            return convertToWindows(path);
        } else {
            return convertToUnix(path);
        }
    }

    public static void main(String[] args) {
        System.out.println(convertWithOs("opt/file"));
    }
}

posted @ 2022-08-04 19:12  码奴生来只知道前进~  阅读(158)  评论(0编辑  收藏  举报