The Clojure namespace
clojure.java.io contains useful functions to work with files. One of those functions is
make-parents. We can pass a
java.io.File instance as arguments or use the same arguments that can be passed to the
file function that is also in this namespace. The function will create all parent directories for the file. The return result is
true if the directories are created (they didn’t exist before) and
false when the directories didn’t have to be created (already exist).
In the following example we see an example of usage of the
(ns mrhaki.io.make-parents (:require [clojure.java.io :refer [make-parents file]] [clojure.test :refer [is]])) ;; make-parents will create the parents directories for a file ;; The function returns true if the directories are created, ;; false if the directories already exist. (let [file (file "tmp" "clojure" "sample.txt")] (is (true? (make-parents file)) "All parent directories are created") (is (false? (make-parents file)) "Second time the directory already exists")) ;; make-parents uses the same arguments as the clojure.java.io.file function (is (true? (make-parents "tmp" "clj" "sample.txt")) "Directories tmp/clj are created")
Written with Clojure 1.10.3.