Reads the file named by f using the encoding enc into a string
and returns it.
;; To access web page. Note the use of http:// ;; prefix user=> (slurp "http://clojuredocs.org") ; This will return the html content of clojuredocs.org
;; On Linux, some JVMs have a bug where they cannot read a file in the /proc ;; filesystem as a buffered stream or reader. A workaround to this JVM issue ;; is to open such a file as unbuffered: (slurp (java.io.FileReader. "/proc/cpuinfo"))
;; You can specify what encoding to use by giving a :encoding param, and an encoding string recognized by your JVM user=> (slurp "/path/to/file" :encoding "ISO-8859-1")
(defn slurp
"Reads the file named by f using the encoding enc into a string
and returns it."
{:added "1.0"}
([f & opts]
(let [opts (normalize-slurp-opts opts)
sb (StringBuilder.)]
(with-open [#^java.io.Reader r (apply jio/reader f opts)]
(loop [c (.read r)]
(if (neg? c)
(str sb)
(do
(.append sb (char c))
(recur (.read r)))))))))
Comments top
1 comment(s) for slurp.
Use slurp also to read an input stream into a string.