I am using this function to write to a file in Clojure.
(defn writelines [file-path lines]
(with-open [wtr (clojure.java.io/writer file-path)]
(doseq [line lines] (.write wtr line))))
But this always generates this error:
IllegalArgumentException No matching method found: write for
class java.io.BufferedWriter in
clojure.lang.Reflector.invokeMatchingMethod (Reflector.java:79)
What am I doing wrong here?
First of all, your function works just fine for many inputs:
However, when you try to pass in something weird we get the error you describe:
This error is because
BufferedWriterhas multiple overloaded versions ofwriteand clojure doesn’t know which one to call. In this case the conflicting ones arewrite(char[])andwrite(String). With inputs like strings ("a") and integers (1) clojure knew to call theStringversion 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
writelinesare indeedStrings or stringifying them using thestrfunction?Also, have a look at the
spitfunction.