On Nov 24, 12:34 am, Stuart Sierra <[EMAIL PROTECTED]>
wrote:
> Honestly, for this kind of low-level stuff I always use the Apache
> Commons libraries, <http://commons.apache.org/>, esp. the Lang and IO
> components. They've got every imaginable stream function, all
> carefully and efficiently implemented. But if you're determined to do
> it in Clojure, loop/recur is the way, as James demonstrated.
> -Stuart Sierra
Yes. commons.io.FileUtils.copyFile is definitely a nicer way :)
I am still learning Java (as an when I need it for Clojure). Thanks
for the pointer.
Thanks James for the pipe-stream.
Here is the copy:
(defn copy [iname oname]
(let [in (new FileInputStream iname)
out (new FileOutputStream oname)
buffer (make-array Byte/TYPE 1024)]
(loop [len (.read in buffer)]
(when (pos? len)
(.write out buffer 0 len)
(recur (.read in buffer))))))
Parth
>
> On Nov 23, 1:35 pm, Parth Malwankar <[EMAIL PROTECTED]> wrote:
>
> > Hello,
>
> > I am trying to translate the following Java
> > snippet into a file copy routine in Clojure.
>
> > public static void copy(InputStream in, OutputStream out) throws
> > IOException {
> > byte[] buffer = new byte[1024];
> > while (true) {
> > int bytesRead = in.read(buffer);
> > if (bytesRead == -1) break;
> > out.write(buffer, 0, bytesRead);
> > }
> > }
>
> > I was barely able to start:
>
> > (defn copy [iname oname]
> > (let [in (new FileInputStream iname)
> > out (new FileOutputStream oname)]
> > nil))
>
> > But now I am totally lost at the "nil". I am not sure how to translate
> > the "while" loop.
>
> > I would appreciate any pointers on how to do this (or maybe a more
> > ideomatic
> > way).
>
> > Thanks.
> > Parth
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---