Java Folder Delete emptyFolder(File folder, boolean ignoreCannotDel)

Here you can find the source of emptyFolder(File folder, boolean ignoreCannotDel)

Description

empty Folder

License

Open Source License

Declaration

static public int emptyFolder(File folder, boolean ignoreCannotDel) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;

import java.io.IOException;

public class Main {
    static public int emptyFolder(File folder, boolean ignoreCannotDel) throws IOException {
        int counter = 0;
        if (folder.exists() && folder.isDirectory()) {
            File[] child = folder.listFiles();
            for (int i = 0; i < child.length; i++) {
                File file = child[i];
                if (file.isDirectory())
                    counter += emptyFolder(file, ignoreCannotDel);
                boolean result = file.delete();
                if (!result && !ignoreCannotDel) {
                    if (!ignoreCannotDel) {
                        throw new IOException("Cannot delete " + file.getAbsolutePath());
                    } else {
                        file.deleteOnExit();
                    }//from   ww w.ja v  a  2 s .  c o m
                } else {
                    counter++;
                }
            }
        }
        return counter;
    }
}

Related

  1. emptyFile(final String urlFile)
  2. emptyFile(String srcFilename)
  3. emptyFolder(File dir)
  4. emptyFolder(File folder)
  5. emptyFolder(File folder)
  6. emptyFolder(File toEmpty)
  7. emptyFolder(final File folder)
  8. emptyFolder(final File folder)