Wednesday, April 14, 2021

Java – How to Delete Files and Folders?

Oracle Java, Core Java, Oracle Java Tutorial and Material, Oracle Java Preparation, Oracle Java Career

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

1. 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.

package com.oraclejavacertified.files.delete;

import java.io.File;

/**

 * How to delete the file in java using File api delete() method.

 * 

 * @author oraclejavacertified.blogspot.com

 *

 */

public class FileDelete {

    public static void main(String[] args) {

        // File deletion success

        String fileName = "src/main/java/com/oraclejavacertified/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/oraclejavacertified/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:

File deleted without any errors for src/main/java/com/oraclejavacertified/files/delete/test.log

File deletion is failed for src/main/java/com/oraclejavacertified/files/delete/no-file.log

2. 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.

package com.oraclejavacertified.files.delete;    

import java.io.File;

/**

 * How to delete the folder in java using File API delete() method.

 * 

 * @author oraclejavacertified.blogspot.com

 *

 */

public class FileDeleteFolder {

    public static void main(String[] args) {

        // Folder deletion not done

        String folderName = "src/main/java/com/oraclejavacertified/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/oraclejavacertified/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:

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.

Related Posts

0 comments:

Post a Comment