First of all, your function works just fine for many inputs:
Clojure 1.3.0
user=> (defn writelines [file-path lines]
(with-open [wtr (clojure.java.io/writer file-path)]
(doseq [line lines] (.write wtr line))))
#'user/writelines
user=> (writelines "foobar" ["a" "b"])
nil
user=> (writelines "quux" [1 2])
nil
However, when you try to pass in something weird we get the error you describe:
user=> (writelines "quux" [#{1}])
IllegalArgumentException No matching method found: write for class java.io.BufferedWriter clojure.lang.Reflector.invokeMatchingMethod (Reflector.java:79)
This error is because BufferedWriter
has multiple overloaded versions of write
and clojure doesn't know which one to call. In this case the conflicting ones are write(char[])
and write(String)
. With inputs like strings ("a"
) and integers (1
) clojure knew to call the String
version of the method, but with something else (e.g. a clojure set, #{1}
) clojure couldn't decide.
How about either ensuring that the inputs to writelines
are indeed String
s or stringifying them using the str
function?
Also, have a look at the spit
function.