Core Java

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.

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
Subscribe
Notify of
guest

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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button