1. 获取当前路径(绝对路径)

package p01;

import java.io.File;
import java.io.IOException;
import java.net.URL;

public class Hello01 {
    public static void main(String[] args) {
        System.out.println(getCurrentPath1());// D:\eclipseJavaWorkspace\eclipse202006Workspace\Hello\bin
        System.out.println(new Hello01().getCurrentPath2());// D:\eclipseJavaWorkspace\eclipse202006Workspace\Hello\bin\p01
        System.out.println(getCurrentPath3());// D:\eclipseJavaWorkspace\eclipse202006Workspace\Hello
        System.out.println(getCurrentPath4());// file:/D:/eclipseJavaWorkspace/eclipse202006Workspace/Hello/bin/
        System.out.println(getCurrentPath5());// D:\eclipseJavaWorkspace\eclipse202006Workspace\Hello\bin
        System.out.println(getCurrentPath6());// D:\eclipseJavaWorkspace\eclipse202006Workspace\Hello
        System.out.println(getCurrentPath7());// /D:/eclipseJavaWorkspace/eclipse202006Workspace/Hello/bin/

        /* 结果:
D:\eclipseJavaWorkspace\eclipse202006Workspace\Hello\bin
D:\eclipseJavaWorkspace\eclipse202006Workspace\Hello\bin\p01
D:\eclipseJavaWorkspace\eclipse202006Workspace\Hello
file:/D:/eclipseJavaWorkspace/eclipse202006Workspace/Hello/bin/
D:\eclipseJavaWorkspace\eclipse202006Workspace\Hello\bin
D:\eclipseJavaWorkspace\eclipse202006Workspace\Hello
D:\eclipseJavaWorkspace\eclipse202006Workspace\Hello\bin
        */
    }

    // 获取当前类的所在工程路径;
    public static String getCurrentPath1() {
        File f = new File(Hello01.class.getResource("/").getPath());
        return f.getPath();
    }

    // 获取当前类的绝对路径;
    public String getCurrentPath2() {
        File f = new File(this.getClass().getResource("").getPath());
        return f.getPath();
    }

    // 获取当前类的所在工程路径;
    public static String getCurrentPath3() {
        File directory = new File("");// 参数为空
        // getCanonicalPath()返回的就是标准的将符号完全解析的路径
        String courseFile = "";
        try {
            courseFile = directory.getCanonicalPath();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return courseFile;
    }

    // 获取当前类的所在工程路径;
    // file:/D:/eclipseJavaWorkspace/eclipse202006Workspace/Hello/bin/
    public static String getCurrentPath4() {
        URL path = Thread.currentThread().getContextClassLoader().getResource("");
        return path.toString();
    }

    public static String getCurrentPath5() {
        return System.getProperty("java.class.path");
    }

    public static String getCurrentPath6() {
        return System.getProperty("user.dir");
    }

    public static String getCurrentPath7() {
        String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();// /D:/eclipseJavaWorkspace/eclipse202006Workspace/Hello/bin/
        String p = new File(path).getAbsolutePath();// D:\eclipseJavaWorkspace\eclipse202006Workspace\Hello\bin
        return p;
    }

}
View Code

相关文章:

  • 2021-06-22
  • 2022-12-23
  • 2021-07-03
  • 2021-10-16
  • 2022-12-23
  • 2021-11-23
  • 2021-11-23
猜你喜欢
  • 2021-10-20
  • 2021-05-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案