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 .
import java.io.*;
public class solution {
public static void main(String args[])
{
File f = new File( "F:\\program" );
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 .
import java.io.*;
public class solution {
public static void main(String args[])
{
File f = new File( "F:\\program1" );
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.
Last Updated :
28 Jan, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...