Java File getParentFile()方法

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

1 语法

public PrintWriter append(char c)

2 参数

3 返回值

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

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.io.File.getParentFile()方法的例子
 */
import java.io.File;

public class Demo {
    public static void main(String[] args) {
        File f = null;
        File f1 = null;
        String v;
        boolean bool = false;

        try {
            // create new file
            f = new File("d:\\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: d:\

热门文章

优秀文章