System.setOut() - Setting Standard Console Output to

Q

How could Java classes direct program messages to the system console, but error messages, say to a file?

✍: FYIcenter

A

The class System has a variable out that represents the standard output, and the variable err that represents the standard error device. By default, they both connected to the system console. But you can change them to files as shown in the this example:

Stream st = new Stream(new
FileOutputStream("output.txt")); 
System.setOut(st);

2007-03-03, 7124👍, 0💬