Hunter0x7c7
2023-09-08 c64f00a1f9a91cb82bd1b8308e0c9b29ef5de76f
src/main/java/com/github/hunter0x7c7/sync/utils/PathUtil.java
@@ -2,6 +2,7 @@
import java.io.File;
import java.net.URL;
import java.net.URLDecoder;
public class PathUtil {
    /**
@@ -13,6 +14,10 @@
     */
    public static final String projectPath = initProjectPathAndDebug();
    public static String getConfigFilePath(String cfgName) {
        return String.format("%s%s%s", projectPath, getFileSeparator(), cfgName);
    }
    /***
     * 获取项目根路径,无论是打包成jar文件。
     * 为了保证调试时获取项目路径,而不是bin路径,增加逻辑: 如果以bin目录接,则返回上一层目录
@@ -20,22 +25,10 @@
     * @return 例如:F:\eclipse\work\JavaFX\AddressApp\build\dist
     */
    private static String initProjectPathAndDebug() {
        if (SystemUtil.isWindows()) {
            System.out.println(".....isWindows....");
        }
        if (SystemUtil.isMacOs()) {
            System.out.println("....isMacOs.....");
        }
        if (SystemUtil.isLinux()) {
            System.out.println("......isLinux...");
        }
        URL url = PathUtil.class.getProtectionDomain().getCodeSource().getLocation();
        String filePath = null;
        try {
            filePath = java.net.URLDecoder.decode(url.getPath(), "utf-8");
            filePath = URLDecoder.decode(url.getPath(), "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
@@ -59,6 +52,10 @@
        return null;
    }
    public static String getFileSeparator() {
        return File.separator;
    }
    /***
     * 这个方法打包位jar文件就无法获取项目路径了。
     * @return
@@ -68,7 +65,7 @@
        File file = new File(realPath);
        realPath = file.getAbsolutePath();//去掉了最前面的斜杠/
        try {
            realPath = java.net.URLDecoder.decode(realPath, "utf-8");
            realPath = URLDecoder.decode(realPath, "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        }