Here you can find the source of deleteDir(File dir)
Parameter | Description |
---|---|
IllegalArgumentException | if the specified file is not a directory |
IOException | if the directory could not be deleted |
public static void deleteDir(File dir) throws IOException
//package com.java2s; /*/*w w w .jav a2s. c o m*/ * The MIT License (MIT) * * Copyright (c) 2010 Technische Universitaet Berlin * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ import java.io.File; import java.io.IOException; import java.io.FileFilter; import java.util.List; import java.util.ArrayList; public class Main { /** * Recursively deletes the specified directory. * * @throws IllegalArgumentException if the specified file is not a directory * @throws IOException if the directory could not be deleted */ public static void deleteDir(File dir) throws IOException { if (!dir.exists()) return; if (!dir.isDirectory()) throw new IllegalArgumentException("Not a directory: " + dir); for (File file : dir.listFiles()) { if (file.isDirectory()) deleteDir(file); else deleteFile(file); } deleteFile(dir); } /** * Returns all files in the specified directory accepted by the specified filter. If * <code>recursive</code> is true, subdirectories are processed recursivly; i.e., files in * descendant directories are returned, too. */ public static File[] listFiles(File dir, FileFilter filter, boolean recursive) { List<File> files = new ArrayList<File>(); scanFiles(dir, filter, recursive, files); return files.toArray(new File[files.size()]); } /** * Deletes the specified file. Unlike {@link File#delete delete} in class * {@link File File}, this method throws an exception if the deletion failed instead of * returning false. * * @throws IOException if the file could not be deleted */ public static void deleteFile(File file) throws IOException { if (!file.delete()) throw new IOException("Could not delete file: " + file); } /** * Auxiliary method to implement {@link #listFiles listFiles}. */ protected static void scanFiles(File dir, FileFilter filter, boolean recursive, List<File> files) { if (!dir.isDirectory()) throw new IllegalArgumentException("scanFiles: not a directory: " + dir); for (File file : dir.listFiles()) { if (filter == null || filter.accept(file)) files.add(file); if (recursive && file.isDirectory()) scanFiles(file, filter, recursive, files); } } }