# File I/O {{tag>clojure file i/o}} ### Reading the Contents of a File as an Entire String (ns clojure.examples.hello (:gen-class)) ;; This program displays Hello World (defn Example [] (def string1 (slurp "Example.txt")) (println string1)) (Example) ### Reading the Contents of a File One Line at a Time (ns clojure.examples.hello (:gen-class)) ;; This program displays Hello World (defn Example [] (with-open [rdr (clojure.java.io/reader "Example.txt")] (reduce conj [] (line-seq rdr)))) (Example) ### Writing ‘to’ Files (ns clojure.examples.hello (:gen-class)) ;; This program displays Hello World (defn Example [] (spit "Example.txt" "This is a string")) ### Writing ‘to’ Files One Line at a Time (ns clojure.examples.hello (:gen-class)) ;; This program displays Hello World (defn Example [] (with-open [w (clojure.java.io/writer "Example.txt" :append true)] (.write w (str "hello" "world")))) (Example) ### Checking to See If a File Exists (ns clojure.examples.hello (:gen-class)) ;; This program displays Hello World (defn Example [] (println (.exists (clojure.java.io/file "Example.txt")))) (Example) ### Reading from the Console user->(read-line) Hello World ## Refs - https://www.tutorialspoint.com/clojure/clojure_file_io.htm