Home » Java programming language

Java File Class boolean isDirectory() method with Example

Java File Class boolean isDirectory() method: Here, we are going to learn about the boolean isDirectory() method of File class with its syntax and example.
Submitted by Preeti Jain, on July 08, 2019

File Class boolean isDirectory()

  • This method is available in package java.io.File.isDirectory().
  • This method is used to check whether the file is specified by filepath is a directory or not.
  • The return type of this method is Boolean i.e. The value of this method is true or false if it returns true that means the file is represented by filepath is a directory else return false so it is not a directory.
  • This method may raise an exception(i.e. Security Exception) if the read access is not given to the file.

Syntax:

    boolean isDirectory(){
    }

Parameter(s):

We don’t pass any object as a parameter in the method of the File.

Return value:

The return type of this method is Boolean i.e. it returns true than in that case file is specified by abstract file path is a directory else returns false so the file is specified is not in a directory.

Java program to demonstrate example of isDirectory() method

// import the File class because we will use File class methods
import java.io.File;

// import the Exception class because it may raise 
// an exception when working with files
import java.lang.Exception;

public class ToCheckFileDirectory {
    public static void main(String[] args) {
        try {
            // Specify the path of file and we use double slashes 
            // to escape '\' character sequence for windows otherwise 
            // it will be considerable as url.

            File file1 = new File("C:\\Users\\computer clinic\\OneDrive\\Articles");
            File file2 = new File("C:\\Users\\computer clinic\\OneDrive\\JavaArticles");

            // By using isDirectory() is used to check whether 
            // the filepath is a directory or not. 
            // It returns true because given filepath is a directory .
            if (file1.isDirectory())
                System.out.println("This filepath " + " " + file1.getAbsolutePath() + " " + "is a directory");
            else
                System.out.println("This filepath " + " " + file1.getAbsolutePath() + " " + "is not a directory");

            // By using isDirectory() is used to check whether 
            // the filepath is a directory or not. It returns false 
            // because given filepath is not a directory .
            if (file2.isDirectory())
                System.out.println("This filepath " + " " + file2.getAbsolutePath() + " " + "is a directory ");
            else
                System.out.println("This filepath " + " " + file2.getAbsolutePath() + " " + "is not a directory");
        } catch (Exception e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}

Output

D:\Programs>javac ToCheckFileDirectory.java

D:\Programs>java ToCheckFileDirectory
This filepath C:\Users\computer clinic\OneDrive\Articles is a directory
This filepath C:\Users\computer clinic\OneDrive\JavaArticles is not a directory


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.