Add method to remove a file and all of its empty parent directories.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 28 Jul 2012 10:10:22 +0000 (12:10 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 28 Jul 2012 10:10:22 +0000 (12:10 +0200)
src/main/java/net/pterodactylus/demoscenemusic/core/DataDirectory.java

index 1574f73..faa169e 100644 (file)
@@ -64,6 +64,25 @@ public class DataDirectory {
                return new File(file, getFileName(id));
        }
 
+       /**
+        * Removes the file and all its directories, up to the
+        * {@link #dataDirectory}.
+        *
+        * @param id
+        *            The ID of the file to remove
+        */
+       public void removeFile(String id) {
+               File file = getFile(id);
+               if (file.delete()) {
+                       File parentDirectory = file.getParentFile();
+                       String lastDirectory = new File(dataDirectory).getAbsolutePath();
+                       while (!lastDirectory.equals(parentDirectory.getAbsolutePath()) && (parentDirectory.listFiles().length == 0)) {
+                               parentDirectory.delete();
+                               parentDirectory = parentDirectory.getParentFile();
+                       }
+               }
+       }
+
        //
        // PRIVATE METHODS
        //