Fahd Shariff wrote:
Raymond DeCampo <rd******@spam.twcny.spam.rr.spam.com.spam> wrote in message news:<KO********************@twister.nyroc.rr.com> ...
Hal,
You can use the System.setOut() method to change the standard output
stream. In your case, you will want to implement a PrintStream that
appends text to the text area.
HTH,
Ray
Which would mean having to change all the existing System.out.print statements....
Fahd,
Why do you say that? The documentation says that System.setOut() will
change the standard output stream, i.e., the stream underlying
System.out. I took this to mean it the equivalent of doing something like
System.out = myPrintStream;
(which you can't do because System.out is final).
I was going to say that I haven't tried this technique, but instead I
decided to give it a try and indeed, it worked:
----- Start SystemOut.java -------------
public class SystemOut
{
public static void main(String args[])
{
System.out.println("hello");
}
}
----- End SystemOut.java -------------
----- Start Redirect.java -------------
import java.io.*;
public class Redirect
{
public static void main(String args[]) throws IOException
{
PrintStream out = null;
try
{
out = new PrintStream(
new BufferedOutputStream(
new FileOutputStream(args[0])));
System.setOut(out);
SystemOut.main(null);
}
finally
{
out.flush();
out.close();
}
}
}
----- End Redirect.java -------------
$ java -cp . Redirect hello.txt
$ cat hello.txt
hello
$ javac Redirect.java SystemOut.java
$ java -cp . SystemOut
hello
$ java -cp . Redirect hello.txt
$ cat hello.txt
hello
Ray
--
XML is the programmer's duct tape.