From 168e9398e35c278595d5d8ae1591e1be564f4539 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Mon, 21 May 2018 12:35:25 +0200 Subject: [PATCH] Add method to delete directory --- .../jivesoftware/smack/util/FileUtils.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/util/FileUtils.java b/smack-core/src/main/java/org/jivesoftware/smack/util/FileUtils.java index 32d6cd46f..9c0562d24 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/util/FileUtils.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/util/FileUtils.java @@ -31,6 +31,7 @@ import java.net.URL; 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; @@ -155,4 +156,24 @@ public final class FileUtils { return false; } } + + public static void deleteDirectory(File root) { + File[] currList; + Stack 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(); + } + } + } }