Last active
August 29, 2015 14:06
-
-
Save kjlubick/b58215395bca9adf2918 to your computer and use it in GitHub Desktop.
Clearing out folders - this deletes all files and folders (recursively) in a folder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static boolean clearOutDirectory(File rootDirectory) | |
{ | |
if (!rootDirectory.exists() || (rootDirectory.isDirectory() && rootDirectory.listFiles().length == 0)) | |
{ | |
return true; | |
} | |
return recursivelyClearDirectory(rootDirectory); | |
} | |
private static boolean recursivelyClearDirectory(File parentDirectory) | |
{ | |
for (File f : parentDirectory.listFiles()) | |
{ | |
if (f.isDirectory()) | |
{ | |
if (!recursivelyClearDirectory(f)) | |
return false; | |
} | |
if (!f.delete()) | |
{ | |
return false; | |
} | |
} | |
return parentDirectory.listFiles().length == 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment