The Clojure-function spit
allows to write data into files, e.g.:
(spit "filename.txt" "content")
It also allows to add content to existing files.
(spit "filename.txt" "content" :append true)
In the documentation ((doc spit)
) it only says that options can be passed to the clojure.java.io/writer
. But (doc clojure.java.io/writer)
does not list allowed options. So is there a "detailed-mode" for documentation available?
I found the :append
-option via http://clojuredocs.org/clojure.core/spit , but I'm sure it is also listed somewhere in the documentation.
List is a structure used to store a collection of data items. In Clojure, the List implements the ISeq interface. Lists are created in Clojure by using the list function.
They can be assigned as values, passed into functions, and returned from functions. It's common to see function definitions in Clojure using defn like (defn foo … ) . However, this is just syntactic sugar for (def foo (fn … )) fn returns a function object.
The slurp command opens a reader on a file and reads all its contents, returning a string. Following is an example of how this can be done. (ns clojure. examples. hello (:gen-class)) ;; This program displays Hello World (defn Example [] (def string1 (slurp "Example.txt")) (println string1)) (Example)
Clojure (/ˈkloʊʒər/, like closure) is a dynamic and functional dialect of the Lisp programming language on the Java platform. Like other Lisp dialects, Clojure treats code as data and has a Lisp macro system.
Probably most of the options are mapped from Java underlying libraries
http://docs.oracle.com/javase/tutorial/essential/io/file.html
By browsing the source code I confirm that :encoding
is legal
https://github.com/clojure/clojure/blob/clojure-1.6.0/src/clj/clojure/java/io.clj#L74-L77
Common options include
:append true to open stream in append mode
:encoding string name of encoding to use, e.g. \"UTF-8\".
I cannot help further as Java isn't my more frequently used language, hope it helps
via clojure.java.io/writer
to make-writer
, so found it in io.clj;
(defprotocol ^{:added "1.2"} IOFactory
"Factory functions that create ready-to-use, buffered versions of
the various Java I/O stream types, on top of anything that can
be unequivocally converted to the requested kind of stream.
Common options include
:append true to open stream in append mode
:encoding string name of encoding to use, e.g. \"UTF-8\".
Callers should generally prefer the higher level API provided by
reader, writer, input-stream, and output-stream."
(^{:added "1.2"} make-reader [x opts] "Creates a BufferedReader. See also IOFactory docs.")
(^{:added "1.2"} make-writer [x opts] "Creates a BufferedWriter. See also IOFactory docs.")
(^{:added "1.2"} make-input-stream [x opts] "Creates a BufferedInputStream. See also IOFactory docs.")
(^{:added "1.2"} make-output-stream [x opts] "Creates a BufferedOutputStream. See also IOFactory docs."))
@Edward, there are just :append
and :encoding
@Jaime Agudo's answer is right, I'd not saw his answer :-(.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With