- 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
How to delete folder and sub folders using Java?
The class named File of the java.io package represents a file or directory (path names) in the system. This class provides various methods to perform various operations on files/directories.
The delete() method of the File class deletes the file/directory represented by the current File object.
This ListFiles() method of the File class returns an array holding the objects (abstract paths) of all the files (and directories) in the path represented by the current (File) object.
Therefore, to delete a folder along with its sub directories and files, you need to define a recursive method.
Example
Following Java program deletes the specified directory recursively −
import java.io.File; public class DeletingFilesRecursively { static void deleteFolder(File file){ for (File subFile : file.listFiles()) { if(subFile.isDirectory()) { deleteFolder(subFile); } else { subFile.delete(); } } file.delete(); } public static void main(String args[]) { String filePath = "E://ExampleDirectory//"; //Creating the File object File file = new File(filePath); deleteFolder(file); System.out.println("Files deleted........"); } }
Output
Files deleted........
Using ApacheCommonsIO
The deleteDirectory() method of the ApacheCommonsIO accepts a file path and directory deletes it recursively.
Maven dependency
<dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version> </dependency>
Example
import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; public class DeletingFilesRecursively2 { public static void main(String args[]) throws IOException { String filePath = "E://ExampleDirectory//"; //Creating the File object File file = new File(filePath); FileUtils.deleteDirectory(file); System.out.println("Files deleted........"); } }
Output
Files deleted........
- Related Articles
- How to delete empty files and folders using PowerShell?
- How to delete hidden files and folders using PowerShell?
- How to get list of all files/folders from a folder in Java?
- How to delete only empty folders in Python?
- How to delete all files and folders from a path in C#?
- How to calculate the size of folder using Java?
- Remove Sub-Folders from the Filesystem in C++
- How to create a directory in project folder using Java?
- How to remove hidden files and folders using Python?
- How to get hidden files and folders using PowerShell?
- How to retrieve files and folders attributes using PowerShell?
- How to change files and folders attributes using PowerShell?
- How to delete a file from the public folder in Laravel?
- How to get the directories (only) from a folder using Java?
- How to delete a MongoDB document using Java?
