File isDirectory() method in Java with Examples
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.
Recommended Posts:
- File getTotalSpace() method in Java with examples
- File getPath() method in Java with Examples
- File list() method in Java with Examples
- File getCanonicalFile() method in Java with Examples
- File setExecutable() method in Java with Examples
- File isAbsolute() method in Java with Examples
- File getParentFile() method in Java with Examples
- File getUsableSpace() method in java with examples
- File canRead() method in Java with Examples
- File canExecute() method in Java with Examples
- File setLastModified() method in Java with Examples
- File setWritable() method in Java with Examples
- File listFiles() method in Java with Examples
- File getParent() method in Java with Examples
- File getName() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

