Java Program to Delete a Directory17 Mar 2025 | 3 min read Java provides the File class for representing a file or directory in the system. The File class is available in java.io package. In order to perform operations on files or directories, the File class provide several useful methods. The delete() method of the File class is one of the most important methods which is used to delete the files and empty directories represented by the current File object. Note: When a directory contains some files, it cannot be deleted directly by using the delete() method. For deleting it, we first empty the directory and then delete the folder. However, we have two ways to delete the files or directories in the system, i.e., using the delete() method of File and using the deleteDirectory() method from commons-io. Let's understand both the ways to understand how we can delete a directory in the system: By using the File.delete() MethodThe delete() method of the File class is used for deleting a directory. Before using the delete() method, we first call a user-defined method, i.e., deleteDirectory(), that will delete the subfile or subdirectory. Let's take an example to understand how we can delete a directory by using the delete() method. ![]() DeleteDirectoryExample1.java Output: ![]() By Using the File.deleteDirectory() MethodThere is another way of deleting a directory in Java. We can use the deleteDirectory() method from the commons-io. In order to use the deleteDirectory() method, we need to add the following dependency to maven project: Let's take an example to understand how we can use commons-io for deleting a directory. DeleteDirectoryExample2.java Output: ![]() |
is a collection of components, i.e., JVM, JRE, and JDK. It integrates the process of interpretation and compilation. It defines all the processes involved in creating a Java program. explains each and every step of how a program is compiled and executed. can...
3 min read
In Java, decision-making logic can be handled through two mechanisms that include the if-else statement combined with the ternary operator. The ternary operator (?:) acts as a compact expression solution that reduces the complexity of conditional statements in code. The handling of several conditions requires different solutions...
5 min read
One of the built-in methods in java.text is getMultiplier(). To get the multiplier to be used in many formats, such as percentage, percentile, etc., the Java class DecimalFomrat is utilized. Syntax: public int getMultiplier() Parameters: No parameters are accepted by this method. Return Value: The multiplier value that can be...
2 min read
? Java main() method that serves as both the program's entry point and the Java Virtual Machine's (JVM) launchpad, is an essential part of Java programs. However, there are situations where a Java program may not contain a main() method. Method Signature public static void main(String[] args) { ...
4 min read
In this section, we will discuss what is a pangram string. We will also create a Java program to check the given string is a pangram or not. What is pangram string? The string is called a pangram if it contains all the alphabets from a to z...
5 min read
Many times, we need to clone an array to backup the original elements of it. We have some special strings and numbers like palindrome number, palindrome string, and Armstrong number, and to check their specialty, we need to clone the array. For example, to check whether...
7 min read
Binary tree is a tree type non-linear data structure that are mainly used for sorting and searching because they store data in hierarchical form. In this section, we will learn the implementation of binary tree data structure in Java. Also, provides a short description of...
64 min read
Eclipse is one of the most outstanding IDEs for Java and Android designers. There are many issues with Eclipse on the off chance that it isn't arranged as expected. You want to endeavour all of them individually. A few clients are kicking the Java Was started...
5 min read
In Java, the ListNode is a crucial data structure used to implement linked lists efficiently. Linked lists are dynamic data structures that consist of nodes, where each node holds a value and a reference to the node in the list. This article aims to provide...
5 min read
In this article, we are going to learn about Open Hashing and Closed Hashing in the Java programming language. By the end of the article, we will cover different parts of the topic, such as why these techniques are used in the Java programming language, what...
22 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India