杰瑞科技汇

Java文件路径如何获取?

获取当前工作目录(JVM 启动目录)

使用 System.getProperty("user.dir") 获取当前 Java 进程的工作目录(通常是执行 java 命令时的目录)。

Java文件路径如何获取?-图1
(图片来源网络,侵删)
public class GetCurrentDir {
    public static void main(String[] args) {
        String currentDir = System.getProperty("user.dir");
        System.out.println("当前工作目录: " + currentDir);
    }
}

获取类文件(.class)所在的路径

(1) 获取当前类的 .class 文件路径

public class GetClassPath {
    public static void main(String[] args) {
        // 获取当前类的.class文件路径(包含包名)
        String classPath = GetClassPath.class.getResource("").getPath();
        System.out.println("当前类的.class文件路径: " + classPath);
        // 获取当前类的.class文件根路径(通常是项目的target/classes目录)
        String classRootPath = GetClassPath.class.getResource("/").getPath();
        System.out.println("类的根路径: " + classRootPath);
    }
}

(2) 获取任意类的 .class 文件路径

public class GetOtherClassPath {
    public static void main(String[] args) throws ClassNotFoundException {
        Class<?> clazz = Class.forName("java.lang.String");
        String classPath = clazz.getResource("").getPath();
        System.out.println("String类的.class文件路径: " + classPath);
    }
}

获取资源文件(res目录下的文件)路径

(1) 从类路径(src/main/resources)获取资源文件

假设资源文件位于 src/main/resources/config.properties

import java.io.InputStream;
public class GetResourcePath {
    public static void main(String[] args) {
        // 方式1:通过ClassLoader获取(适用于非Class对象)
        InputStream inputStream = GetResourcePath.class.getClassLoader().getResourceAsStream("config.properties");
        System.out.println("通过ClassLoader获取资源流: " + (inputStream != null));
        // 方式2:通过Class对象获取(适用于与类同包的资源)
        String resourcePath = GetResourcePath.class.getResource("config.properties").getPath();
        System.out.println("通过Class对象获取资源路径: " + resourcePath);
        // 方式3:获取根目录下的资源(如src/main/resources下的文件)
        String rootResourcePath = GetResourcePath.class.getResource("/config.properties").getPath();
        System.out.println("获取根目录资源路径: " + rootResourcePath);
    }
}

(2) 注意事项

  • 资源文件必须位于类路径(src/main/resourcestarget/classes)中。
  • 路径中的 表示类路径的根目录(src/main/resources)。
  • 如果资源文件在 JAR 包中,需使用 getResourceAsStream 而非 getPath()

获取 JAR 包路径

(1) 获取当前 JAR 包的路径

import java.net.URL;
import java.nio.file.Paths;
public class GetJarPath {
    public static void main(String[] args) {
        URL jarUrl = GetJarPath.class.getProtectionDomain().getCodeSource().getLocation();
        String jarPath = Paths.get(jarUrl.toURI()).toString();
        System.out.println("JAR包路径: " + jarPath);
    }
}

(2) 获取 JAR 包中的资源文件

import java.io.InputStream;
public class GetJarResource {
    public static void main(String[] args) {
        InputStream inputStream = GetJarResource.class.getClassLoader().getResourceAsStream("META-INF/MANIFEST.MF");
        System.out.println("JAR包中MANIFEST.MF是否存在: " + (inputStream != null));
    }
}

获取绝对路径(推荐使用 Path 类)

Java 7+ 推荐使用 java.nio.file.Path 处理路径:

import java.nio.file.Path;
import java.nio.file.Paths;
public class GetAbsolutePath {
    public static void main(String[] args) {
        // 解析绝对路径
        Path path = Paths.get("/home/user/file.txt");
        System.out.println("绝对路径: " + path.toAbsolutePath());
        // 解析相对路径(相对于当前工作目录)
        Path relativePath = Paths.get("subdir/file.txt");
        System.out.println("相对路径的绝对路径: " + relativePath.toAbsolutePath());
    }
}

需求场景 方法 示例
获取当前工作目录 System.getProperty("user.dir") System.getProperty("user.dir")
获取类的 .class 文件路径 Class.getResource("") MyClass.class.getResource("").getPath()
获取类路径根目录 Class.getResource("/") MyClass.class.getResource("/").getPath()
获取资源文件路径 ClassLoader.getResource() MyClass.class.getClassLoader().getResource("file.txt").getPath()
获取 JAR 包路径 Class.getProtectionDomain().getCodeSource().getLocation() MyClass.class.getProtectionDomain().getCodeSource().getLocation()
处理绝对路径 Paths.get().toAbsolutePath() Paths.get("file.txt").toAbsolutePath()

根据你的具体需求选择合适的方法,如果是资源文件,优先使用 ClassLoader.getResource()Class.getResource();如果是文件系统路径,推荐使用 java.nio.file.Path

Java文件路径如何获取?-图2
(图片来源网络,侵删)
分享:
扫描分享到社交APP
上一篇
下一篇