以下实例演示了使用 File 类的 file.getParent() 方法来获取文件的上级目录:
- /*
- author by shouce.ren
- Main.java
- */
- import java.io.File;
- public class Main {
- public static void main(String[] args) {
- File file = new File("C:/File/demo.txt");
- String strParentDirectory = file.getParent();
- System.out.println("文件的上级目录为 : "
- + strParentDirectory);
- }
- }
以上代码运行输出结果为:
- 文件的上级目录为 : File