Java File Path Delete deleteDir(File dir)

Here you can find the source of deleteDir(File dir)

Description

delete Dir

License

Apache License

Declaration

private static boolean deleteDir(File dir) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.*;

public class Main {
    private static boolean deleteDir(File dir) {
        try {/*from   ww w  .j a  va2s  . c om*/
            return ((deleteFromDir(dir)) && (dir.delete()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean deleteFromDir(String dirPath) {
        File file = new File(dirPath);
        return deleteFromDir(file);
    }

    public static boolean deleteFromDir(File dir) {
        if (!dir.exists()) {
            return true;
        }
        if (!(dir.isDirectory())) {
            return false;
        }

        File[] files = dir.listFiles();
        for (File file : files) {
            if (!(delete(file))) {
                return false;
            }
        }

        return true;
    }

    public static boolean delete(String pathname) {
        File file = new File(pathname);
        return delete(file);
    }

    public static boolean delete(File file) {
        if (!file.exists()) {
            return true;
        }
        if (file.isFile()) {
            return file.delete();
        } else if (file.isDirectory()) {
            return deleteDir(file);
        }

        return false;
    }
}

Related

  1. deleteDir(File dir)
  2. deleteDir(File dir)
  3. deleteDir(File dir)
  4. deleteDir(File dir)
  5. deleteDir(File dir)
  6. deleteDir(File dir)
  7. deleteDir(File dir)
  8. deleteDir(File dir)
  9. deleteDir(File dir)