Skip to content

clojure.core/slurp

View this page on ClojureDocs

Type: function Added: Clojure 1.0 Examples: 7 Runnable: 2
([f & opts])

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

by pkolloch on . May have evaluation errors.
by OnesimusUnbound on
by Havvy on . May have evaluation errors.
by jafingerhut on . May have evaluation errors.
by nils.grunwald on . May have evaluation errors.
by freckletonj on
by mhmdsalem1993 on . May have evaluation errors.

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:

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.