目录

File getParentFile()

描述 (Description)

java.io.File.getParentFile()方法返回此抽象路径名父项的抽象路径名,如果此路径名未指定父目录,则返回null。

声明 (Declaration)

以下是java.io.File.getParentFile()方法的声明 -

public File getParentFile()

参数 (Parameters)

NA

返回值 (Return Value)

此方法返回此抽象路径名指定的父目录的抽象路径名,如果路径名未指定父目录,则返回null。

异常 (Exception)

NA

例子 (Example)

以下示例显示了java.io.File.getParentFile()方法的用法。

package com.iowiki;
import java.io.File;
public class FileDemo {
   public static void main(String[] args) {      
      File f = null;
      File f1 = null;
      String v;
      boolean bool = false;
      try {
         // create new file
         f = new File("C:\\test.txt");
         // returns abstract parent pathname
         f1 = f.getParentFile();
         // absolute path from abstract pathname
         v = f1.getAbsolutePath();
         // true if the file path exists
         bool = f.exists();
         // if file exists
         if(bool) {
            // prints
            System.out.print("Parent file path: "+v);
         }
      } catch(Exception e) {
         // if any error occurs
         e.printStackTrace();
      }
   }
}

让我们编译并运行上面的程序,这将产生以下结果 -

Parent file path: C:\
↑回到顶部↑
WIKI教程 @2018