Java: Easy way to log the output of the System.out to a file

bobcpg

Senior member
Nov 14, 2001
951
0
0
I am programming in java and my app outputs text to the command window. Is there an easy way to log all the output to a file.

thanks
-bob
 

Yomicron

Golden Member
Mar 5, 2002
1,735
1
81
you could create your own print method. In your new method you can write the output to a file and print to the screen. Then instead of calling System.out.print() in your program, just call your new method.
 

AgentEL

Golden Member
Jun 25, 2001
1,327
0
0
Are you using a command line?

If so, you can simply do:

"java Class > output.txt"
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
the command line fix AgentEL recommends is the easiest.

Another way is to make sure that none of your code references System.out literally. Make it all talk to a generic PrintWriter and then configure that PrintWriter in one spot to where you want it to go (to a file or to System.out or wherever). If you want to get fancy check out log4j. That's really the ultimate in configurable logging and output printing.
 

Kilrsat

Golden Member
Jul 16, 2001
1,072
0
0
This is how to redirect the System.out and System.err streams from inside your application.