Java如何获取当前的工作目录?

user.dir如果要查找Java程序的当前工作目录,可以使用名为的系统属性。

package org.nhooo.example.io;

public class CurrentDirectoryExample {
    public static void main(String[] args) {
        // 获得当前工作目录的系统属性键。
        String USER_DIR_KEY = "user.dir";
        String currentDir = System.getProperty(USER_DIR_KEY);

        System.out.println("Working Directory: " + currentDir);
    }
}

结果示例:

Working Directory: C:\Users\nhooo\Documents\Projects\nhooo.project