clojure.core/slurp¶
Opens a reader on f and reads all its contents, returning a string. See clojure.java.io/reader for a complete list of supported arguments.
Examples¶
Note by mjg123¶
Use slurp also to read an input stream into a string.
Note by roryokane¶
With link: “See clojure.java.io/reader for a complete list of supported arguments.�
According to those docs, here are the supported types for f, the object to read:
ReaderBufferedReaderInputStreamFileURIURLSocket- byte arrays (
byte[]) - character arrays (
char[]) String
slurp can read objects of any of these types into a string.
Note by nevesjorgelucio¶
(defn slurp-bytes
"Slurp the bytes from a slurpable thing"
[x]
(with-open [out (java.io.ByteArrayOutputStream.)]
(clojure.java.io/copy (clojure.java.io/input-stream x) out)
(.toByteArray out)))
Note by didibus¶
Valid options include:
:encoding string name of encoding to use, e.g. \"UTF-8\".
Note by piranha¶
(defn slurp-norm
"When the file is incoming with \r\n newlines"
[file]
(with-open [rdr (io/reader file)]
(str/join "\n" (line-seq rdr))))
See also¶
Content from the matching ClojureDocs page, with authors credited on each contribution.