From 565ad78c862c9fefcba5fb35fd07e6d2a25746a1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 28 Jul 2012 12:10:22 +0200 Subject: [PATCH] Add method to remove a file and all of its empty parent directories. --- .../demoscenemusic/core/DataDirectory.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/main/java/net/pterodactylus/demoscenemusic/core/DataDirectory.java b/src/main/java/net/pterodactylus/demoscenemusic/core/DataDirectory.java index 1574f73..faa169e 100644 --- a/src/main/java/net/pterodactylus/demoscenemusic/core/DataDirectory.java +++ b/src/main/java/net/pterodactylus/demoscenemusic/core/DataDirectory.java @@ -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 // -- 2.7.4