Here you can find the source of deleteFolderRec(File path, boolean alsoDeleteGivenFolder)
Parameter | Description |
---|---|
path | Directory to be deleted |
alsoDeleteGivenFolder | Flag, indicating whether the directory that was passed in as first argument shall be deleted along with its contents or not. |
public static boolean deleteFolderRec(File path, boolean alsoDeleteGivenFolder)
//package com.java2s; import java.io.File; public class Main { /**//from w ww. ja v a 2 s . com * This function will recursively delete directories and files. * * @param path Directory to be deleted * @param alsoDeleteGivenFolder Flag, indicating whether the directory that * was passed in as first argument shall be deleted along with its contents * or not. * @return <tt>true</tt> if deletion was successfully completed,<br> * <tt>false</tt> otherwise. Parts of the given folder might already be deleted. */ public static boolean deleteFolderRec(File path, boolean alsoDeleteGivenFolder) { boolean ok; if (path.exists()) { ok = true; if (path.isDirectory()) { File[] files = path.listFiles(); for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { deleteFolderRec(files[i], true); } else { files[i].delete(); } } if (alsoDeleteGivenFolder) { ok = ok && path.delete(); } } } else { ok = false; } return ok; } }