From 8eef617aac7b077421d4aa9c84a2df189af6ee17 Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Thu, 8 Oct 2009 21:29:25 -0700 Subject: java-utils: Add delete-file and delete-file-recursively functions Also added test for delete-file. Refs #33 Signed-off-by: Chouser --- src/clojure/contrib/java_utils.clj | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'src/clojure/contrib/java_utils.clj') diff --git a/src/clojure/contrib/java_utils.clj b/src/clojure/contrib/java_utils.clj index 579d1128..7af4bc93 100644 --- a/src/clojure/contrib/java_utils.clj +++ b/src/clojure/contrib/java_utils.clj @@ -175,6 +175,23 @@ (doto (as-properties m) (.store f #^String comments))))) +(defn delete-file + "Delete file f. Raise an exception if it fails unless silently is true." + [f & [silently]] + (or (.delete (file f)) + silently + (throw (java.io.IOException. (str "Couldn't delete " f))))) + +(defn delete-file-recursively + "Delete file f. If it's a directory, recursively delete all its contents. +Raise an exception if any deletion fails unless silently is true." + [f & [silently]] + (let [f (file f)] + (if (.isDirectory f) + (doseq [child (.listFiles f)] + (delete-file-recursively child silently))) + (delete-file f silently))) + (defmulti #^{:doc "Coerces argument (URL, URI, or String) to a java.net.URL." :arglists '([arg])} -- cgit v1.2.3-18-g5258