Skip to content

Commit

Permalink
Merge pull request #239 from vanitasvitae/fileutils
Browse files Browse the repository at this point in the history
Add method to delete directory
  • Loading branch information
Flowdalic committed May 22, 2018
2 parents 27c77fc + 168e939 commit a89f345
Showing 1 changed file with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.Stack;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -155,4 +156,24 @@ public static boolean writeFile(File file, CharSequence content) {
return false;
}
}

public static void deleteDirectory(File root) {
File[] currList;
Stack<File> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
if (stack.lastElement().isDirectory()) {
currList = stack.lastElement().listFiles();
if (currList != null && currList.length > 0) {
for (File curr : currList) {
stack.push(curr);
}
} else {
stack.pop().delete();
}
} else {
stack.pop().delete();
}
}
}
}

0 comments on commit a89f345

Please sign in to comment.