Java.io.File.isDirectory() Method

Advertisements


Description

The java.io.File.isDirectory() checks whether the file denoted by this abstract pathname is a directory.

Declaration

Following is the declaration for java.io.File.isDirectory() method:

public boolean isDirectory()

Parameters

  • NA

Return Value

The method returns true if and only if the file denoted by this abstract pathname is a directory else the method returns false.

Exception

  • SecurityException -- If a security manager exists and its SecurityManager.checkRead(java.lang.String) method denies read access to the file

Example

The following example shows the usage of java.io.File.isDirectory() method.

package com.tutorialspoint;

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();
      }
   }
}

Let us compile and run the above program, this will produce the following result:

c: is directory? true
c:\test.txt is directory? false



Advertisements
Advertisements