java怎么獲取當(dāng)前目錄路徑

小億
113
2023-11-15 09:46:32
欄目: 編程語言

在Java中,你可以使用`System.getProperty("user.dir")`方法來獲取當(dāng)前工作目錄的路徑。

下面是一個(gè)示例代碼片段演示如何獲取當(dāng)前目錄路徑:

public class CurrentDirectoryExample {
    public static void main(String[] args) {
        String currentDir = System.getProperty("user.dir");
        System.out.println("當(dāng)前目錄路徑:" + currentDir);
    }
}

在上述代碼中,`System.getProperty("user.dir")`方法返回一個(gè)字符串,其中包含當(dāng)前工作目錄的路徑。然后,我們使用`System.out.println()`方法將該路徑打印到控制臺(tái)。

運(yùn)行上述代碼將輸出當(dāng)前目錄的路徑。

0