Here you can find the source of deleteFolderRecursively(File path, boolean includeSelf)
Parameter | Description |
---|---|
path | a parameter |
includeSelf | also delete the passed directory itself? |
Parameter | Description |
---|---|
IOException | an exception |
public static void deleteFolderRecursively(File path, boolean includeSelf) throws IOException
//package com.java2s; import java.io.File; import java.io.IOException; public class Main { /**//from w ww . j av a 2 s . c om * Deletes directory at path recursively. * * @param path * @param includeSelf also delete the passed directory itself? * * @throws IOException */ public static void deleteFolderRecursively(File path, boolean includeSelf) throws IOException { if (!path.isDirectory()) throw new IllegalArgumentException("The passed file is not a directory."); File[] files = path.listFiles(); if (files == null) return; for (int i = 0; i < files.length; i++) { File f = files[i]; if (f.isFile()) { if (!f.delete()) { throw new IOException("Couldn't delete file: " + f.getAbsolutePath()); } } else { deleteFolderRecursively(f, true); } } if (includeSelf && !path.delete()) { throw new IOException("Couldn't delete directory: " + path.getAbsolutePath()); } } /** * Deletes directory at path recursively. * * @param path * @param includeSelf also delete the passed directory itself? * * @throws IOException */ public static void deleteFolderRecursively(String path, boolean includeSelf) throws IOException { deleteFolderRecursively(new File(path), includeSelf); } /** * Deletes directory at path recursively. * * @param path * * @throws IOException */ public static void deleteFolderRecursively(File path) throws IOException { deleteFolderRecursively(path, true); } /** * Deletes directory at path recursively. * * @param path * * @throws IOException */ public static void deleteFolderRecursively(String path) throws IOException { deleteFolderRecursively(new File(path), true); } }