Here you can find the source of deleteRecursively(File fRoot)
Parameter | Description |
---|---|
fRoot | The root directory to be deleted. |
Parameter | Description |
---|---|
IOException | Thrown if the deletion operation failed. |
public static void deleteRecursively(File fRoot) throws IOException
//package com.java2s; //License from project: Apache License import java.io.File; import java.io.IOException; public class Main { /**//from ww w . j a v a 2 s.co m * Deletes all files and directories under the given directory. * * @param fRoot The root directory to be deleted. * * @throws IOException Thrown if the deletion operation failed. */ public static void deleteRecursively(File fRoot) throws IOException { if (fRoot == null) { throw new NullPointerException("Root folder is null."); } // Check if the root is a file. if (fRoot.isFile()) { // Try to delete it. if (!fRoot.delete()) { // The deletion failed. throw new IOException("Unable to delete file '" + fRoot.getAbsolutePath() + "'"); } return; } // List all files and directories under this directory. File[] faFiles = fRoot.listFiles(); for (int iIndex = 0; iIndex < faFiles.length; iIndex++) { File fFile = faFiles[iIndex]; // Call this method recursively deleteRecursively(fFile); } // Try to delete folder. if (!fRoot.delete()) { // The deletion failed. throw new IOException("Unable to delete folder '" + fRoot.getAbsolutePath() + "'"); } } }