posted 23 years ago
It's probably easiest to do this outside java, using system coimmands to redirect the output of the java process. On Unix Bourne/Korn shells or NT for example, use something like:
java myClass >outputFileName.txt 2>&1
The first > redirects standard output to the named file; the "2>&1" basically says to take the error output (signified by 2) and redirect it to the same file standard output (signified by 1) is going. This way everything goes to the same place. If you prefer you can direct standard and error output to different places. Consult documentation for whatever system you are using - it's certainly worthwhile to know how to do this easily.
It's also possible to do this from within Java:
<code><pre>
PrintStream out = new PrintStream(new FileOutputStream("outfile.txt));
System.setOut(out);
System.setErr(out);
</pre></code>
Same effect, and platform-neutral. You must recompile to reedirect to a different file - but you could write a main() method to accept a command-line argument for the file to redirect to (if any), for maximum flexibility. Enjoy.
"I'm not back." - Bill Harding, Twister