From: David ‘Bombe’ Roden Date: Sat, 28 Jul 2012 10:10:22 +0000 (+0200) Subject: Add method to remove a file and all of its empty parent directories. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=565ad78c862c9fefcba5fb35fd07e6d2a25746a1;p=demoscenemusic.git Add method to remove a file and all of its empty parent directories. --- 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 //