温馨提示×

java怎么获取文件父路径

小亿
93
2023-12-23 20:23:09
栏目: 编程语言

在Java中,可以使用File类的getParent()方法来获取文件的父路径。

以下是一个示例代码:

import java.io.File;

public class Main {
    public static void main(String[] args) {
        // 创建一个文件对象
        File file = new File("C:\\test\\example.txt");

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

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

以上代码将输出文件的父路径:C:\test

0