Java Directory Clear cleanDirectory(IProgressMonitor monitor, File directory, File base, int step)

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

Description

clean Directory

License

Open Source License

Declaration

public static void cleanDirectory(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  ww  w.j  av  a2s  .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 {
    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);
            }
        }
    }

    /**
     * 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);
    }
}

Related

  1. cleanDirectory(File directory)
  2. cleanDirectory(File path)
  3. cleanDirectory(final File directory)
  4. cleanDirectory(final File directory)
  5. cleanDirectory(IProgressMonitor monitor, File directory)
  6. cleanDirectory(String dir)
  7. cleanDirectory(String dirPath)
  8. clearDirctory(File dir)
  9. clearDirectory(File dir)