In Java how to Delete Files, Folders on Windows, Mac OS X and Linux?

Sometime back I’ve written an article on how to remove /tmp or unnecessary files / folder on Linux automatically via script? Now it’s time to write the same utility for Windows environment.

In this tutorial we will go over all steps in details to delete Files and Folders on Windows OS, Mac OS X and Linux.

Let’s get started:

  1. Create file CrunchifyDeleteWindowsFileFolder.java
  2. Create crunchifyDeleteWindowsFolder(List of Directories) which first check for if directory exists or not? If exists then it will delete all files under it.
  3. Create crunchifyDeleteFiles(file) which deletes file.

Look at these two directories and 5 files:

  1. c:\crunchify folder
    1. crunchify-1.txt
    2. crunchify-2.txt
    3. crunchify-3.txt
  2. c:\temp folder
    1. crunchify-4.txt
    2. crunchify-5.txt
  3. For Mac OS
    1. /Users/appshah/Downloads/file.ppsx
  4. For Linux
    1. /tmp/crunchify-file.txt
package crunchify.com.tutorial;

import java.io.File;
import java.io.IOException;

/**
 * @author Crunchify.com
 *
 */

public class CrunchifyDeleteWindowsFileFolder {
        public static void main(String[] args) {

                // For Windows Provide Location: c:\\crunchify, c:\\temp
                // For Mac OS X Provide Location: /Users/appshah/Downloads/file.ppsx
                // For Linux Provide Location: /tmp/crunchify-file.txt
                String[] crunchifyDir = { "c:\\crunchify", "c:\\temp", "/Users/appshah/Downloads/file.ppsx",
                                "/tmp/crunchify-file.txt" };
                String result = crunchifyDeleteWindowsFolder(crunchifyDir);
                System.out.println("Result: " + result);
        }

        public static void crunchifyDeleteFiles(File myFile) throws IOException {

                if (myFile.isDirectory()) {
                        // Returns an array of strings naming the files and directories in the directory denoted by this abstract
                        // pathname.
                        String crunchifyFiles[] = myFile.list();

                        for (String file : crunchifyFiles) {
                                // Creates a new File instance from a parent abstract pathname and a child pathname string.
                                File fileDelete = new File(myFile, file);

                                // recursion
                                crunchifyDeleteFiles(fileDelete);
                        }

                } else {
                        // Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a directory,
                        // then the directory must be empty in order to be deleted.
                        myFile.delete();
                        System.out.println("File is deleted : " + myFile.getAbsolutePath());
                }
        }

        public static String crunchifyDeleteWindowsFolder(String[] crunchifyDirectories) {

                try {
                        for (String crunchifyDir : crunchifyDirectories) {
                                File directory = new File(crunchifyDir);

                                // Tests whether the file or directory denoted by this abstract pathname exists.
                                if (!directory.exists()) {
                                        System.out.println("File does not exist: " + directory);
                                } else {
                                        try {
                                                // recursion approached
                                                crunchifyDeleteFiles(directory);
                                                System.out.println("Cleaned Up Files Under Directory: " + directory + "\n");
                                        } catch (IOException e) {
                                                e.printStackTrace();
                                                System.exit(0);
                                        }
                                }
                        }
                        return "Execution Complete";
                } catch (Exception e) {
                        return "Execution Failed";
                }
        }

}

As I ran this program on Windows machine I got this Result:

File is deleted : c:\crunchify\crunchify-1.txt
File is deleted : c:\crunchify\crunchify-2.txt
File is deleted : c:\crunchify\crunchify-3.txt
Cleaned Up Files Under Directory: c:\crunchify

File is deleted : c:\temp\crunchify-4.txt
File is deleted : c:\temp\crunchify-5.txt
Cleaned Up Files Under Directory: c:\temp

File does not exist: /Users/appshah/Downloads/file.ppsx
File does not exist: /tmp/crunchify-file.txt

Result: Execution Complete

The post In Java how to Delete Files, Folders from Windows, Mac OS X and Linux OS? appeared first on Crunchify.