Java Delete Directory deleteDirectory(IProgressMonitor monitor, File directory, File base, int step)

Here you can find the source of deleteDirectory(IProgressMonitor monitor, File directory, File base, int step)

Description

Recursively delete a directory.

License

Open Source License

Parameter

Parameter Description
directory directory to delete

Exception

Parameter Description
IOException in case deletion is unsuccessful

Declaration

public static void deleteDirectory(IProgressMonitor monitor,
        File directory, File base, int step) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2016 Chen Chao(cnfree2000@hotmail.com).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from w  w  w. j a  v a 2 s  .c  om*/
 *  Chen Chao  - initial API and implementation
 *******************************************************************************/

import java.io.File;

import java.io.FileNotFoundException;

import java.io.IOException;

import org.eclipse.core.runtime.IProgressMonitor;

public class Main {
    /**
     * Recursively delete a directory.
     * 
     * @param directory
     *            directory to delete
     * @throws IOException
     *             in case deletion is unsuccessful
     */
    public static void deleteDirectory(IProgressMonitor monitor,
            File directory, File base, int step) throws IOException {
        if (!directory.exists()) {
            return;
        }

        cleanDirectory(monitor, directory, base, step);
        if (!directory.delete()) {
            String message = "Unable to delete directory " + directory + "."; //$NON-NLS-1$ //$NON-NLS-2$
            throw new IOException(message);
        }
    }

    public static void deleteDirectory(IProgressMonitor monitor,
            File directory, int step) throws IOException {
        deleteDirectory(monitor, directory, directory, step);
    }

    public static void cleanDirectory(IProgressMonitor monitor,
            File directory, File base, int step) throws IOException {
        if (!directory.exists()) {
            String message = directory + " does not exist"; //$NON-NLS-1$
            throw new IllegalArgumentException(message);
        }

        if (!directory.isDirectory()) {
            String message = directory + " is not a directory"; //$NON-NLS-1$
            throw new IllegalArgumentException(message);
        }

        IOException exception = null;

        boolean isPackage = false;

        File[] files = directory.listFiles();
        for (int i = 0; i < files.length; i++) {
            File file = files[i];
            if (!isPackage && file.isFile()) {
                isPackage = true;
            }
            try {
                forceDelete(monitor, file, base, step);
            } catch (IOException ioe) {
                exception = ioe;
            }
        }

        if (isPackage) {
            if (monitor != null) {
                monitor.worked(step);
            }
        }

        if (null != exception) {
            throw exception;
        }
    }

    public static void forceDelete(IProgressMonitor monitor, File file,
            File base, int step) throws IOException {
        if (file.isDirectory()) {
            deleteDirectory(monitor, file, base, step);
        } else {
            if (monitor != null) {
                String taskName = file.getAbsolutePath().substring(
                        base.getAbsolutePath().length()
                                + new Long(System.currentTimeMillis())
                                        .toString().length() + 2);
                monitor.subTask(taskName);
            }
            if (!file.exists()) {
                throw new FileNotFoundException(
                        "File does not exist: " + file); //$NON-NLS-1$
            }
            if (!file.delete()) {
                String message = "Unable to delete file: " + file; //$NON-NLS-1$
                throw new IOException(message);
            }
        }
    }
}

Related

  1. deleteDirectory(final File directory)
  2. deleteDirectory(final File directory)
  3. deleteDirectory(final String dir)
  4. deleteDirectory(final String targetDir)
  5. deleteDirectory(IProgressMonitor monitor, File directory)
  6. deleteDirectory(String dir)
  7. deleteDirectory(String directory)
  8. deleteDirectory(String directory)
  9. deleteDirectory(String directoryName)