1
0

More than 1 year has passed since last update.

Javaで「ファイルかディレクトリかを判定する(Files.isDirectory)」の動作を確認してみた

Posted at

概要

Javaで「ファイルかディレクトリかを判定する(Files.isDirectory)」の動作を確認してみました。以下のページを参考にしました。

実装

以下のファイルを作成しました。

JSample2_1.java
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.Files;

class JSample2_1{
  public static void main(String[] args){
    Path p1 = Paths.get("doc/report");
    Path p2 = Paths.get("doc/manual");
    Path p3 = Paths.get("doc/memo");

    checkDirectory(p1);
    checkDirectory(p2);
    checkDirectory(p3);
  }

  private static void checkDirectory(Path p){
    System.out.print(p.getFileName() + "は");
    if (Files.isDirectory(p)){
      System.out.println("ディレクトリです");
    }else{
      if (Files.exists(p)){
        System.out.println("ファイルです");
      }else{
        System.out.println("存在しません");
      }
    }
  }
}

以下のコマンドを実行しました。

$ javac JSample2_1.java 
$ java JSample2_1 
reportはファイルです
manualはディレクトリです
memoは存在しません

まとめ

何かの役に立てばと。

1
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
1
0