温馨提示×

java怎么获取当前目录路径

小亿
89
2023-11-15 09:46:32
栏目: 编程语言

在Java中,你可以使用`System.getProperty("user.dir")`方法来获取当前工作目录的路径。

下面是一个示例代码片段演示如何获取当前目录路径:

public class CurrentDirectoryExample {
    public static void main(String[] args) {
        String currentDir = System.getProperty("user.dir");
        System.out.println("当前目录路径:" + currentDir);
    }
}

在上述代码中,`System.getProperty("user.dir")`方法返回一个字符串,其中包含当前工作目录的路径。然后,我们使用`System.out.println()`方法将该路径打印到控制台。

运行上述代码将输出当前目录的路径。

0