Java-io-file-isdirectory

提供:Dev Guides
移動先:案内検索

Java.io.File.isDirectory()メソッド

説明

  • java.io.File.isDirectory()*は、この抽象パス名が示すファイルがディレクトリかどうかをチェックします。

宣言

以下は* java.io.File.isDirectory()*メソッドの宣言です-

public boolean isDirectory()

パラメーター

NA

戻り値

この抽象パス名が示すファイルがディレクトリである場合にのみ、メソッドはtrueを返します。それ以外の場合、メソッドはfalseを返します。

例外

*SecurityException* -セキュリティマネージャが存在し、そのSecurityManager.checkRead(java.lang.String)メソッドがファイルへの読み取りアクセスを拒否する場合

次の例は、java.io.File.isDirectory()メソッドの使用法を示しています。

package com.finddevguides;

import java.io.File;

public class FileDemo {
   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? false
c:\test.txt is directory? false