溫馨提示×

java怎么獲取文件父路徑

小億
148
2023-12-23 20:23:09
欄目: 編程語言

在Java中,可以使用File類的getParent()方法來獲取文件的父路徑。

以下是一個示例代碼:

import java.io.File;

public class Main {
    public static void main(String[] args) {
        // 創(chuàng)建一個文件對象
        File file = new File("C:\\test\\example.txt");

        // 獲取文件的父路徑
        String parentPath = file.getParent();

        // 打印父路徑
        System.out.println("父路徑:" + parentPath);
    }
}

以上代碼將輸出文件的父路徑:C:\test

0