1.3.0 permalink Arrow_down_16x16

slurp

clojure.core

  • (slurp 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.

5 Examples top

  • user=> (spit "blubber.txt" "test")
    nil
    user=> (slurp "blubber.txt")
    "test"
  • ;; To access web page. Note the use of http://
    ;; prefix
    
    user=> (slurp "http://clojuredocs.org")
    ; This will return the html content of clojuredocs.org
  • ;; Access absolute location on Windows
    
    user=> (slurp "C:\\tasklists.xml")
    
  • ;; 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")
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:6017 top

(defn 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."
  {: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)))))))))
Vars in clojure.core/slurp:
Used in 0 other vars

Comments top

1 comment(s) for slurp.

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.

Log in to add a comment.