Java File Delete delete(File file)

Here you can find the source of delete(File file)

Description

delete

License

Apache License

Declaration

public static boolean delete(File file) 

Method Source Code


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

import java.io.*;

public class Main {

    public static boolean delete(String pathname) {
        File file = new File(pathname);
        return delete(file);
    }// w w  w.  j  av  a 2 s  .  c  om

    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;
    }

    private static boolean deleteDir(File dir) {
        try {
            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;
    }
}

Related

  1. delete(File file)
  2. delete(File file)
  3. delete(File file)
  4. delete(File file)
  5. delete(File file)
  6. delete(File file)
  7. delete(File file)
  8. delete(File file)
  9. delete(File file)