- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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(); }
- Related Articles
- Java Program to check if a file or directory is readable
- Check for file or directory in Java
- Golang Program to check if a file is directory or a file
- How to check if a file is a directory or a regular file in Python?
- Check whether the given file is an existing file in Java
- Check whether we can write to a file in Java
- Check whether the file is hidden or not in Java
- Java Program to check whether a file exists or not
- Check if a directory is not empty in Java
- How to check if a File Type Exists in a Directory?
- How to search a file in a directory in java
- Check whether the file can be read in Java
- Specify a path for a file or a directory in Java
- C# Program to check whether a directory exists or not
- Java Program to rename a file or directory

Advertisements