Here you can find the source of deleteDirectory(IProgressMonitor monitor, File directory)
Parameter | Description |
---|---|
monitor | a parameter |
directory | directory to delete |
Parameter | Description |
---|---|
IOException | in case deletion is unsuccessful |
public static void deleteDirectory(IProgressMonitor monitor, File directory) throws IOException
//package com.java2s; /******************************************************************************* * Copyright (c) 2011 cnfree./* w w w . j av a2 s. c o m*/ * 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: * cnfree - initial API and implementation *******************************************************************************/ import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.eclipse.core.runtime.IProgressMonitor; public class Main { /** * Recursively delete a directory. * * @param monitor * * @param directory * directory to delete * @throws IOException * in case deletion is unsuccessful */ public static void deleteDirectory(IProgressMonitor monitor, File directory) throws IOException { if (!directory.exists()) { return; } cleanDirectory(monitor, directory); if (!directory.delete()) { String message = "Unable to delete directory " + directory + "."; throw new IOException(message); } } /** * Clean a directory without deleting it. * * @param monitor * * @param directory * directory to clean * @throws IOException * in case cleaning is unsuccessful */ public static void cleanDirectory(IProgressMonitor monitor, File directory) throws IOException { if (!directory.exists()) { String message = directory + " does not exist"; throw new IllegalArgumentException(message); } if (!directory.isDirectory()) { String message = directory + " is not a directory"; throw new IllegalArgumentException(message); } IOException exception = null; File[] files = directory.listFiles(); for (int i = 0; i < files.length; i++) { if (monitor.isCanceled()) return; File file = files[i]; try { forceDelete(monitor, file); } catch (IOException ioe) { exception = ioe; } } if (null != exception) { throw exception; } } public static List<File> listFiles(File directory, String[] extensions, int depth) { List<File> files = new ArrayList<File>(); File[] children = directory.listFiles(); if (children != null) { for (int i = 0; i < children.length; i++) { if (children[i].isDirectory()) { if (depth > 0) files.addAll(listFiles(children[i], extensions, depth - 1)); } else { String suffix = children[i].getName(); if (suffix.lastIndexOf('.') == -1) continue; suffix = suffix.substring(suffix.lastIndexOf('.') + 1); if (extensions != null) { for (int j = 0; j < extensions.length; j++) { if (extensions[j].equalsIgnoreCase(suffix)) { files.add(children[i]); } } } else { files.add(children[i]); } } } } return files; } public static void forceDelete(IProgressMonitor monitor, File file) throws IOException { if (file.isDirectory()) { if (monitor.isCanceled()) return; deleteDirectory(monitor, file); } else { if (!file.exists()) { throw new FileNotFoundException("File does not exist: " + file); } if (!file.delete()) { String message = "Unable to delete file: " + file; throw new IOException(message); } } } }