Java Delete Directory deleteFiles(File[] files)

Here you can find the source of deleteFiles(File[] files)

Description

delete Files

License

Open Source License

Declaration

public static void deleteFiles(File[] files) throws IOException 

Method Source Code

//package com.java2s;

import java.io.File;

import java.io.IOException;

public class Main {
    public static void deleteFiles(File[] files) throws IOException {
        for (File file : files) {
            if (file.isDirectory()) {
                deleteFolder(file);/*from w  w  w . j  a v  a 2 s.  c o  m*/
            } else {
                file.delete();
            }
        }
    }

    /**
     * Delete a folder and its contents
     * @param afolder -  a folder
     */
    public static void deleteFolder(File afolder) throws IOException {
        if (afolder == null) {
            return;
        }

        // Not root directories
        // This way does not work where afolder = new File("aFolder");
        // File parent = afolder.getParentFile();
        File parent = new File(afolder.getAbsolutePath()).getParentFile();
        if (parent == null) {
            throw new IOException("Cannot delete root folder"); //$NON-NLS-1$
        }

        if (afolder.exists() && afolder.isDirectory()) {
            //delete content of directory:
            File[] files = afolder.listFiles();
            int count = files.length;
            for (int i = 0; i < count; i++) {
                File f = files[i];
                if (f.isFile()) {
                    f.delete();
                } else if (f.isDirectory()) {
                    deleteFolder(f);
                }
            }
            afolder.delete();
        }
    }
}

Related

  1. deleteFiles(File file)
  2. deleteFiles(File folder)
  3. deleteFiles(File folder)
  4. deleteFiles(File parent, boolean incParent)
  5. deleteFiles(File[] files)
  6. deleteFiles(File[] intermediateFiles)
  7. deleteFiles(FileFilter filter)
  8. deleteFiles(final ArrayList files)
  9. deleteFiles(final File directory, final String regexPattern)