 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check whether a file is a directory in Java
The method java.io.File.isDirectory() checks whether a file with the specified abstract path name is a directory or not. This method returns true if the file specified by the abstract path name is a directory and false otherwise.
A program that demonstrates this is given as follows −
Example
import java.io.File; public class Demo {    public static void main(String[] args) {       try {          File file = new File("demo1.txt");          file.createNewFile();          System.out.println("Is directory? " + file.isDirectory());       } catch(Exception e) {          e.printStackTrace();       }    } } The output of the above program is as follows −
Output
Is directory? false
Now let us understand the above program.
The method java.io.File.isDirectory() checks whether the file is a directory or not and the boolean value that is returned by the method is printed. A code snippet that demonstrates this is given as follows −
try {    File file = new File("demo1.txt");    file.createNewFile();    System.out.println("Is directory? " + file.isDirectory()); } catch(Exception e) {    e.printStackTrace(); }Advertisements
 