Java如何确定路径名是否为目录?

要确定抽象路径名是否为目录,我们可以使用File.isDirectory()方法。这是示例代码。

package org.nhooo.example.io;

import java.io.File;

public class IsDirectoryExample {
    public static void main(String[] args) {
        // 创建File的实例。
        File file = new File("/home/foo/bar");

        // 通过调用检查抽象路径名是否为目录
        // File类的isDirectory()方法。
        if (file.isDirectory()) {
            System.out.println("This file is a directory.");
        } else {
            System.out.println("This is just an ordinary file.");
        }
    }
}