Link to home
Start Free TrialLog in
Avatar of yongsing
yongsing

asked on

Prevent echos to screen

Is it possible to call System.setOut()/System.setErr() to prevent all subsequent printouts (echos) to the screens? Then at some point in the program, I want to restore back the echos. Can this be done? Thanks!
Avatar of Mick Barry
Mick Barry
Flag of Australia image

sure use setOut(System.out), setErr(System.err) to put it back.
Avatar of yongsing
yongsing

ASKER

How do you turn off the echo first? Call System.setOut() with what? I suppose I need to save System.out somewhere first?
try pointing it to null, otherwise send it to /dev/null or similiar
apache commons has a NullOutputStream
>> I suppose I need to save System.out somewhere first

No, I don't think so.
>> try pointing it to null

That will cause NullPointerException when there is a System.out.println() statement somewhere.
Correct. You will need to save System.out too in a PrintStream reference.

PrintStream ps = System.out ;

System.setOut ( whatever ) ; // if it is null, it will throw an NPE, right - try the NullOutputStream that objects suggested

System.setOut ( ps ) ;
The following codes will work. It prints 1 and 3 to screen, and 2 is written to a file.

    PrintStream out = System.out;
    System.out.println("1");
    System.setOut(new PrintStream(new FileOutputStream("t")));
    System.out.println("2");
    System.setOut(out);
    System.out.println("3");

I don't really want standard output to be re-direct to a file. Is it possible to redirect it to nothing?
> Is it possible to redirect it to nothing?

yes, see my earlier comment
if you don't want to use commons then easy enuf to write your own.
Make sure you don't leave the PrintStream open - do close it in some finally block later. And delete the temporary file which is created (you can also use File.createTempFile () to make it easier).
>> you can also use File.createTempFile () to make it easier

But I still need to delete the file once I'm done.

>> if you don't want to use commons then easy enuf to write your own

How to write my own?
SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> f.deleteOnExit();

It doesn't seem to work. The file is still there when the program exits with System.exit(0);
Strange, it is supposed to work :) anyway, you can always manually delete it in a finally block as a sure-shot work-around.
All you need to do is start your program thus:

(Windows)
java X 2>&1 >nul

(*nix)

java X 2>&1 >/dev/null

But CEHJ, will that allow redirection of stream later in the code if required?
But CEHJ, will that allow redirection of stream later in the code if required?
I didn't know it *was* required ;-)
>> Then at some point in the program, I want to restore back the echos.

;-)