possibility to recursively delete directories
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sat, 2 Nov 2013 21:18:14 +0000 (23:18 +0200)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sat, 2 Nov 2013 21:18:14 +0000 (23:18 +0200)
src/main/java/eu/svjatoslav/commons/file/IOHelper.java

index a6dd05f..5b4e634 100644 (file)
@@ -17,6 +17,28 @@ import java.io.IOException;
 
 public class IOHelper {
 
+       /**
+        * Deletes files and directories recursively. WARNING!!! Follows symlinks!!!
+        */
+       public static void deleteRecursively(final File file) throws IOException {
+               if (file.isDirectory()) {
+
+                       for (final File subFile : file.listFiles())
+                               deleteRecursively(subFile);
+
+                       if (!file.delete())
+                               throw new FileNotFoundException("Failed to delete directory: "
+                                               + file);
+
+                       return;
+               }
+
+               if (file.isFile())
+                       if (!file.delete())
+                               throw new FileNotFoundException("Failed to delete file: "
+                                               + file);
+       }
+
        public static byte[] getFileContents(final File file)
                        throws FileNotFoundException, IOException {