Open In App

File isDirectory() method in Java with Examples

Last Updated : 28 Jan, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

The isDirectory() function is a part of File class in Java . This function determines whether the is a file or directory denoted by the abstract filename is Directory or not.The function returns true if the abstract file path is Directory else returns false.

Function signature:

public boolean isDirectory()

Syntax:

file.isDirectory()

Parameters: This method does not accept any parameter.

Return Value: The function returns boolean value representing whether the abstract file path is a directory or not

Exception: This method throws Security Exception if the read access to the file is denied

Below programs illustrates the use of isDirectory() function:

Example 1: The file “F:\\program.txt” is a existing file in F: directory .




// Java program to demonstrate
// isDirectory() method of File Class
  
import java.io.*;
  
public class solution {
    public static void main(String args[])
    {
  
        // Get the file
        File f = new File("F:\\program");
  
        // Check if the specified path
        // is a directory or not
        if (f.isDirectory())
            System.out.println("Directory");
        else
            System.out.println("is not Directory");
    }
}


Output:

Directory

Example 2: The file “F:\\program1” is a non existing directory in F: directory .




// Java program to demonstrate
// isDirectory() method of File Class
  
import java.io.*;
  
public class solution {
    public static void main(String args[])
    {
  
        // Get the file
        File f = new File("F:\\program1");
  
        // Check if the specified path
        // is a directory or not
        if (f.isDirectory())
            System.out.println("Directory");
        else
            System.out.println("Not a Directory");
    }
}


Output:

Not a Directory

Note: The programs might not run in an online IDE. Please use an offline IDE and set the path of the file.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads