扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

java怎么获取当前文件路径

扬州沐宇科技
2023-10-21 10:35:45
Java

在Java中,可以使用System.getProperty("user.dir")来获取当前文件的路径。这会返回一个字符串,表示当前的工作目录。另外,可以使用File类来获取当前文件的路径,如下所示:

import java.io.File;

public class Main {
    public static void main(String[] args) {
        File currentFile = new File("");
        String currentPath = currentFile.getAbsolutePath();
        System.out.println("当前文件路径:" + currentPath);
    }
}

这段代码创建了一个空的File对象,然后使用getAbsolutePath()方法获取当前文件的绝对路径,并将其打印出来。

扫码添加客服微信