Show pageOld revisionsBacklinksBack to top This page is read only. You can view the source, but not change it. Ask your administrator if you think this is wrong. # File I/O {{tag>clojure file i/o}} ### Reading the Contents of a File as an Entire String <code> (ns clojure.examples.hello (:gen-class)) ;; This program displays Hello World (defn Example [] (def string1 (slurp "Example.txt")) (println string1)) (Example) </code> ### Reading the Contents of a File One Line at a Time <code> (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) </code> ### Writing ‘to’ Files <code> (ns clojure.examples.hello (:gen-class)) ;; This program displays Hello World (defn Example [] (spit "Example.txt" "This is a string")) </code> ### Writing ‘to’ Files One Line at a Time <code> (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) </code> ### Checking to See If a File Exists <code> (ns clojure.examples.hello (:gen-class)) ;; This program displays Hello World (defn Example [] (println (.exists (clojure.java.io/file "Example.txt")))) (Example) </code> ### Reading from the Console <code> user->(read-line) Hello World </code> ## Refs - https://www.tutorialspoint.com/clojure/clojure_file_io.htm open/file-i-o.txt Last modified: 2024/10/05 06:15by 127.0.0.1