3

The following code :

(spit "/Users/nha/tmp/spit.txt" (.getBytes "hello"))

produces a file containing "[B@71e054bc", which is independent of the content ("hello" in this case) since this is the JVM representation of the address of a byte array.

However the following works (taken from this SO post):

  (clojure.java.io/copy
   (.getBytes "hello")
   (java.io.File. "/Users/nha/tmp/spit.txt"))

And the file then contains the correct content "hello".

Why does spit behave like this ? Is there a way to expand it's behaviour for bytes ?

Community
  • 1
  • 1
nha
  • 17,623
  • 13
  • 87
  • 133
  • 1
    You could just wrap the bytes in a string, `(spit "spit.txt" (String. (.getBytes "hello")))`? Difficult to see what use that would be, especially since raw bytes don’t have an encoding and the output will depend on the platform. – glts Feb 22 '16 at 15:12

2 Answers2

3

the reason is that the content is passed to str

you can check that via (source spit):

user=> (source spit)
(defn spit
  "Opposite of slurp.  Opens f with writer, writes content, then
  closes f. Options passed to clojure.java.io/writer."
  {:added "1.2"}
  [f content & options]
  (with-open [^java.io.Writer w (apply jio/writer f options)]
    (.write w (str content))))
;            - ^^^ - here

therefor you get the string representation of the byte-array written

EDIT: and since spit the "inverse" of slurp which gives you a string, it makes sense and is consistent behaviour

birdspider
  • 3,034
  • 1
  • 16
  • 25
3

This problem isn’t particular to byte arrays.

(spit "spit.txt" (Object.))
(slurp "spit.txt")              ;; => "java.lang.Object@90b293d"

Looking at the source of spit you’ll see that it simply tries to obtain the string representation of the argument before writing it out.

You could try to wrap any byte array in a new string before writing, but take care to select the proper encoding.

;; platform-dependent!
(spit "spit.txt" (String. b))

;; better
(spit "spit.txt" (String. b java.nio.charset.StandardCharsets/UTF_8))
glts
  • 21,808
  • 12
  • 73
  • 94