概要
指定されたパスがディレクトリであるかどうかを判定する。
isDirectory()
メソッドは以下のように定義されている。
public boolean isDirectory()
このメソッドは、呼び出されたFile
オブジェクトがディレクトリである場合にtrue
を返し、ディレクトリでない場合にはfalse
を返す。
例
import java.io.File;
public class IsDirectoryExample {
public static void main(String[] args) {
// ファイルとディレクトリのパスを指定してFileオブジェクトを作成
File file = new File("/path/to/file_or_directory");
// isDirectory()メソッドでディレクトリかどうかを判定
if (file.isDirectory()) {
System.out.println("指定されたパスはディレクトリです。");
} else {
System.out.println("指定されたパスはディレクトリではありません。");
}
}
}
上記の例では、/path/to/file_or_directory
に指定されたパスがディレクトリであれば、「指定されたパスはディレクトリです。」と表示される。
ディレクトリでない場合は、「指定されたパスはディレクトリではありません。」と表示される。
①指定されたパスがディレクトリの場合
/path/to/file_or_directory/
├── file1.txt
└── subdirectory/
├── file2.txt
└── file3.txt
実行結果
指定されたパスはディレクトリです。
②指定されたパスがファイルの場合
/path/to/file_or_directory.txt
実行結果
指定されたパスはディレクトリではありません。