- 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
Delete a directory in Java
The method java.io.File.delete() can be used to delete the directory with the required abstract path name. This method requires no parameters and it returns true on the successful deletion of the 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) { boolean flag; String directory = "D:\a"; File file = new File(directory); flag = file.mkdir(); System.out.println("The directory is created? " + flag); flag = file.delete(); System.out.println("The directory is deleted? " + flag); } }
The output of the above program is as follows −
Output
The directory is created? true The directory is deleted? true
Now let us understand the above program.
The directory is created and then deleted using the method java.io.File.delete(). Then the boolean value returned by this method is printed. A code snippet that demonstrates this is given as follows −
boolean flag; String directory = "D:\a"; File file = new File(directory); flag = file.mkdir(); System.out.println("The directory is created? " + flag); flag = file.delete(); System.out.println("The directory is deleted? " + flag);
- Related Articles
- Delete the file or directory in Java
- Delete file or directory on termination in Java
- Java Program to delete file or directory
- Java program to delete all the files in a directory recursively (only files)
- Java Program to delete a file or directory when the program ends
- How to delete a Python directory effectively?
- How to delete multiple files in a directory in Python?
- How to delete all files in a directory with Python?
- Create a directory in Java
- Golang program to delete empty and non-empty directory
- How to disable delete permission of File and Directory in Linux?
- Check whether a file is a directory in Java
- How to search a file in a directory in java
- Java program to List all files in a directory and nested sub-directory - Recursive approach
- Get Java Home Directory

Advertisements