Opposite of slurp. Opens f with writer, writes content, then closes f. Options passed to clojure.java.io/writer.
user=> (spit "event.log" "test 1\n" :append true) nil user=> (spit "event.log" "test 2\n" :append true) nil user=> (println (slurp "event.log")) test 1 test 2 nil
(defn append-to-file "Uses spit to append to a file specified with its name as a string, or anything else that writer can take as an argument. s is the string to append." [file-name s] (spit file-name s :append true))
;;Create a record and save a log message to a log file ;;Constructor with side effects ;;define a Person record (defrecord Person [fname lname]) ;;define a function to save a log message into the log.txt using spit and :append (defn log-entry [msg] (spit "log.txt" (apply str msg "\n") :append true)) ;;build the constructor which: 1) log the message; 2)create a Person (defn make-person [fname lname] (log-entry (apply str "[log] New Person created : " lname "," fname)) (->Person fname lname)) ;;create a person (def person (make-person "John" "Smith")) ;;print the content of the log.txt to the console (println (slurp "log.txt"))
; ClojureCLR doesn't use :append, use this instead (spit "hi.txt" "Test 1\n" :file-mode System.IO.FileMode/Append)
;; Spitting map, adds "," between key-value pairs (spit "hi.txt" {:a 1 :b 2}) ;; Now let's read it (slurp "hello.txt") ;; => "{:a 1, :b 2}"
Opens a reader on f and reads all its contents, returning a string. See clojure.java.io/reader for...
Attempts to coerce its argument into an open java.io.Writer. Default implementations always retur...
Sequentially read and evaluate the set of forms contained in the file.
Given the same arg(s) as for file, creates all parent directories of the file they represent.