Java File isDirectory()方法

java.io.File.isDirectory() 检查表示此抽象路径名的文件是否是一个目录。

1 语法

public boolean isDirectory()

2 参数

3 返回值

当且仅当表示此抽象路径名的文件是一个目录该方法返回true,否则该方法返回false

4 示例 

package com.yiidian;

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

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

        try {
            // create new file
            f = new File("c:");

            // true if the file path is directory, else false
            bool = f.isDirectory();

            // get the path
            path = f.getPath();

            // prints
            System.out.println(path+" is directory? "+ bool);

            // create new file
            f = new File("c:/test.txt");

            // true if the file path is directory, else false
            bool = f.isDirectory();

            // get the path
            path = f.getPath();

            // prints
            System.out.print(path+" is directory? "+bool);

        } catch(Exception e) {
            // if any error occurs
            e.printStackTrace();
        }
    }
}

输出结果为:

c: is directory? true
c:\test.txt is directory? false

热门文章

优秀文章