Here you can find the source of recursivelyDelete(final File root, final boolean deleteRoot)
Parameter | Description |
---|---|
root | directory (or file) whose contents will be deleted |
deleteRoot | true deletes root directory, false deletes only its contents. |
Parameter | Description |
---|---|
IOException | if something goes wrong, which may leave thingsin an inconsistent state. |
public static void recursivelyDelete(final File root, final boolean deleteRoot) throws IOException
//package com.java2s; import java.io.File; import java.io.IOException; public class Main { /**//from w ww .java 2s .co m * Recursively deletes the contents of the given directory (and * possibly the directory itself). * * @param root directory (or file) whose contents will be deleted * @param deleteRoot true deletes root directory, false deletes only * its contents. * * @throws IOException if something goes wrong, which may leave things * in an inconsistent state. */ public static void recursivelyDelete(final File root, final boolean deleteRoot) throws IOException { if (root.isDirectory()) { final File[] contents = root.listFiles(); for (final File child : contents) { recursivelyDelete(child, true); } } if (deleteRoot) { if (!root.delete()) { throw new IOException("Could not delete directory " + root); } } } /** * Convenience version of {@link #recursivelyDelete(File, boolean)} that * deletes the given root directory as well. */ public static void recursivelyDelete(final File root) throws IOException { recursivelyDelete(root, true); } }