Home » Java » Core Java » Java – How to Delete Files and Folders?

About Venkatesh Nukala

Venkatesh Nukala
Venkatesh Nukala is a Software Engineer working for Online Payments Industry Leading company. In my free time, I would love to spend time with family and write articles on technical blogs. More on JavaProgramTo.com

Java – How to Delete Files and Folders?

A quick guide to delete the files and folders in java with example programs.

1. Overview

In this tutorial, We will learn how to delete the files and folders in java.

Let us learn the example programs on file deletion and folder removal in java.

2. Java Files Delete Example

First, Use delete() method on the file object to delete the file. Returns true if the file is delete successfully and else return false if there are any failures.

In the below program, we have taken two files test.log file is present in the location and no-file.log does not exists on the location.

Let us see the behaviour of delete() method.

01
02
03
04
05
06
07
08
09
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
39
40
41
42
43
44
45
package com.javaprogramto.files.delete;
 
import java.io.File;
 
/**
 * How to delete the file in java using File api delete() method.
 *
 * @author JavaProgramTo.com
 *
 */
public class FileDelete {
 
    public static void main(String[] args) {
 
        // File deletion success
        String fileName = "src/main/java/com/javaprogramto/files/delete/test.log";
         
        File file = new File(fileName);
         
        boolean isFileDeleted = file.delete();
         
        if(isFileDeleted) {
            System.out.println("File deleted without any errors for "+fileName);
        } else {
            System.out.println("File deletion is failed");
        }
         
        // File deletion error.
         
        fileName = "src/main/java/com/javaprogramto/files/delete/no-file.log";
         
        file = new File(fileName);
         
        isFileDeleted = file.delete();
         
        if(isFileDeleted) {
            System.out.println("File deleted without any errors for "+fileName);
        } else {
            System.out.println("File deletion is failed for "+fileName);
        }
 
 
    }
 
}

Output:

1
2
File deleted without any errors for src/main/java/com/javaprogramto/files/delete/test.log
File deletion is failed for src/main/java/com/javaprogramto/files/delete/no-file.log

3. Java Delete Folder Example

Next, we will try to delete the folder which is having the files and next empty folder deletion using delete() method.

01
02
03
04
05
06
07
08
09
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
39
40
41
42
package com.javaprogramto.files.delete;
 
import java.io.File;
 
/**
 * How to delete the folder in java using File API delete() method.
 *
 * @author JavaProgramTo.com
 *
 */
public class FileDeleteFolder {
 
    public static void main(String[] args) {
 
        // Folder deletion not done
        String folderName = "src/main/java/com/javaprogramto/files/delete";
         
        File file = new File(folderName);
         
        boolean isFileDeleted = file.delete();
         
        if(isFileDeleted) {
            System.out.println("Folder with files is deleted");
        } else {
            System.out.println("Folder with files is not deleted");
        }
         
        // Empty Folder deletion success .
         
        folderName = "src/main/java/com/javaprogramto/files/emptyfolder";
         
        file = new File(folderName);
         
        isFileDeleted = file.delete();
         
        if(isFileDeleted) {
            System.out.println("Empty Folder deleted ");
        } else {
            System.out.println("Empty Folder deletion is failed for "+folderName);
        }
    }
}

Output:

1
2
Folder with files is not deleted
Empty Folder deleted

Note: if the folder is empty then only folder will be deleted and folder which has files won’t be deleted. But, we can delete the files folder after deleting all files.

4. Conclusion

In this article, we’ve seen how to delete the files and folder in java with examples.

Published on Java Code Geeks with permission by Venkatesh Nukala, partner at our JCG program. See the original article here: Java – How to Delete Files and Folders?

Opinions expressed by Java Code Geeks contributors are their own.

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments