forked from dharmanshu1921/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeletedirectory.java
38 lines (30 loc) · 1021 Bytes
/
deletedirectory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Java program to delete a directory
import java.io.File;
class DeleteDirectory {
// function to delete subdirectories and files
public static void deleteDirectory(File file)
{
// store all the paths of files and folders present
// inside directory
for (File subfile : file.listFiles()) {
// if it is a subfolder,e.g Rohan and Ritik,
// recursiley call function to empty subfolder
if (subfile.isDirectory()) {
deleteDirectory(subfile);
}
// delete files and empty subfolders
subfile.delete();
}
}
public static void main(String[] args)
{
// store file path
String filepath = "C:\\GFG";
File file = new File(filepath);
// call deleteDirectory function to delete
// subdirectory and files
deleteDirectory(file);
// delete main GFG folder
file.delete();
}
}