0
0

More than 1 year has passed since last update.

【JavaGold】isDirectoryメソッド

Posted at

概要

指定されたパスがディレクトリであるかどうかを判定する。

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
実行結果
指定されたパスはディレクトリではありません。
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0