Checking if a directory exists

Clojure:
Checking if a directory exists

How to:

Clojure, being a JVM language, can utilize Java’s java.io.File class for this purpose. You don’t need any third-party library for such a basic operation. Here’s how you can do it:

(import 'java.io.File)

(defn directory-exists? [dir-path]
  (let [dir (File. dir-path)]
    (.exists dir)))

;; Usage example
(println (directory-exists? "/path/to/your/directory")) ;; true or false

This function, directory-exists?, takes a directory path as a string and returns true if the directory exists and false otherwise. This is achieved by creating a File object with the directory path and then calling the .exists method on this object.

In addition to raw Java interop, you can use Clojure libraries that abstract away some of the Java boilerplate. One such library is clojure.java.io. However, for checking if a directory exists, you would still use the File class, but you might find the library useful for other file operations. Example:

(require '[clojure.java.io :as io])

(defn directory-exists?-clojure [dir-path]
  (.exists (io/file dir-path)))

;; Example usage
(println (directory-exists?-clojure "/another/path/to/check")) ;; true or false

This version is quite similar but uses the Clojure io/file function to create the File object. This method blends more naturally into Clojure codebases by leveraging Clojure’s library for IO operations, rather than directly interfacing with Java classes.