Skip to main content

Java Program to delete any file or directory

The following code is to delete any file or directory and all of its contents. The deleted file or directory will be deleted permanently same as using SHIFT+DEL keys.
Code:-

import java.io.*;
import java.nio.file.*;
import java.util.*;
public class Delete {
    Scanner scan = new Scanner(System.in);
    String pth;
    char ch;
    boolean empty;
    public void input() {
        try {
            System.out.printf("PATH: ");
            pth = scan.nextLine();
        } catch (InputMismatchException e) {
            System.err.println("Error Occur!\n" + e.getMessage());
            System.exit(0);
        }
    }
    public void check(Path delPath) {
        try {
            if (Files.isHidden(delPath)) {
                System.out.println("Path Leads To An Hidden File/Directory!");
                System.out.printf("Still Wanna Continue Y/N? ");
                pth = scan.next();
                ch = pth.charAt(0);
                pth = Character.toString(ch);
            }
            if (Files.notExists(delPath)) {
                System.err.println("Invalid Path!");
                System.exit(0);
            }
            if (pth.equalsIgnoreCase("N")) {
                System.out.println("You chose not to continue!");
                System.exit(0);
            }
        } catch (Exception e) {
            System.err.println("Error Occur!\n" + e.getMessage());
            System.exit(0);
        }
    }
    public void compute(Path delPath) {
        check(delPath);
        boolean deleted;
        try {
            delPath = Paths.get(pth);
            deleted = Files.deleteIfExists(delPath);
            if (deleted == true) {
                System.out.println("RESULT: DELETED!");
                System.exit(0);
            } else {
                System.out.println("RESULT: NOT DELETED!");
                System.exit(0);
            }
        } catch (DirectoryNotEmptyException e) {
            System.out.println("Error Occur!\n" + e.getMessage());
            empty = false;
        } catch (IOException e) {
            System.err.println("Error Occured!\n" + e.getMessage());
            System.exit(0);
        } finally {
            try {
                if (empty == false) {
                    System.out.printf("PERMISSION: DIRECTORY NOT EMPTY CONTINUE Y/N? ");
                    pth = scan.next();
                    ch = pth.charAt(0);
                    pth = Character.toString(ch);
                    if (pth.equalsIgnoreCase("N")) {
                        System.out.println("You chose not to continue!");
                        System.exit(0);
                    }
                    File delFolder = new File(delPath.toString());
                    deleted = deleteNonEmptyDir(delFolder);
                    if (deleted == true) {
                        System.out.println("RESULT: DELETED!");
                    } else {
                        System.exit(0);
                    }
                }
            } catch (Exception e) {
                System.err.println("Error Occur!\n" + e.getMessage());
                System.exit(0);
            }
        }
    }
    public boolean deleteNonEmptyDir(File dir) {
        try {
            String[] list = dir.list();
            for (int i = 0; i < list.length; i++) {
                File temp = new File(dir.getAbsolutePath() + "\\" + list[i]);
                boolean del = temp.delete();
                if (del == false) {
                    boolean success = deleteNonEmptyDir(new File(dir, list[i]));
                    if (!success) {
                        return false;
                    }
                }
            }
        } catch (Exception e) {
            System.err.println("Error Occur!\n" + e.getMessage());
            System.exit(0);
        }
        return dir.delete();
    }
    public static void main(String[] args) {
        Delete del = new Delete();
        del.input();
        Path p = Paths.get("C:\\");
        del.compute(p);
    }
}

Comments