From: Timothy J. H. <ti...@cs...> - 2006-04-23 02:13:16
|
On Apr 22, 2006, at 7:44 PM, Geoffrey Knauth wrote: > I'm running a command and I'd like to capture the output in a string. > For example: > > (out (run (cmd (wc -l ,filename)))) > > I tried wrapping a ByteArrayOutputStream in a PrintStream, but that > didn't seem to do anything. You need to create a StringWriter object w, wrap it in a PrinterWriter object p, and pass that object as the second parameter to the "out" method. Invoking .toString on the w object will give you your string.... Here's an example... > (define w (java.io.StringWriter.)) > (out (run (cmd (ls -l "."))) (java.io.PrintWriter. w)) 0 > (display (.toString w)) total 155416 drwx------ 45 tim tim 1530 22 Apr 09:56 Desktop drwx------ 67 tim tim 2278 18 Apr 07:58 Documents drwx------ 44 tim tim 1496 10 Apr 17:47 Library drwx------ 6 tim tim 204 1 Apr 19:49 Movies drwx------ 5 tim tim 170 30 Jan 2005 Music drwx------ 4 tim tim 136 7 Jan 20:08 Pictures ---snip--- Best, ---Tim--- > > Geoffrey > -- > Geoffrey S. Knauth | http://knauth.org/gsk > > > > > ------------------------------------------------------- > Using Tomcat but need to do more? Need to support web services, > security? > Get stuff done quickly with pre-integrated technology to make your job > easier > Download IBM WebSphere Application Server v.1.0.1 based on Apache > Geronimo > http://sel.as-us.falkag.net/sel? > cmd=lnk&kid=120709&bid=263057&dat=121642 > _______________________________________________ > Jscheme-user mailing list > Jsc...@li... > https://lists.sourceforge.net/lists/listinfo/jscheme-user |