Welcome! Please see the About page for a little more info on how this works.

+1 vote
in IO by

I want to use some libs in IO for text file and binary file,not using ugly synax like .write,.read.
then, how about libs for network socket??

1 Answer

+2 votes
edited by

For files, resources and other streams have a look at clojure.java.io namespace.

Note that java interop is idiomatic Clojure — it's a valid tool, not a last resort, so you should not look at is as ugly. Here is a network socket example:

$ clj
Clojure 1.10.1
user=> (def socket (java.net.Socket. "" 5555))
user=> (require '[clojure.java.io :as io])
user=> (def out (io/writer (.getOutputStream socket)))
user=> (def in (io/reader (.getInputStream socket)))
user=> (.write out "(+ 1 2 3)\n")
user=> (.flush out)
user=> (.readLine in)
"user=> 6"

You can test it by starting socket repl on port 5555 in a separate terminal:

$ clj -J-Dclojure.server.repl='{:port 5555 :accept clojure.core.server/repl}'