yongsing
asked on
Prevent echos to screen
Is it possible to call System.setOut()/System.set Err() 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!
sure use setOut(System.out), setErr(System.err) to put it back.
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
apache commons has a NullOutputStream
>> I suppose I need to save System.out somewhere first
No, I don't think so.
No, I don't think so.
ASKER
>> try pointing it to null
That will cause NullPointerException when there is a System.out.println() statement somewhere.
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 ) ;
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 ) ;
ASKER
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?
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.
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).
ASKER
>> 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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
>> f.deleteOnExit();
It doesn't seem to work. The file is still there when the program exits with System.exit(0);
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
(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.
;-)
;-)