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!
LVL 9
yongsingAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mick BarryJava DeveloperCommented:
sure use setOut(System.out), setErr(System.err) to put it back.
yongsingAuthor Commented:
How do you turn off the echo first? Call System.setOut() with what? I suppose I need to save System.out somewhere first?
Mick BarryJava DeveloperCommented:
try pointing it to null, otherwise send it to /dev/null or similiar
apache commons has a NullOutputStream
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Mayank SAssociate Director - Product EngineeringCommented:
>> I suppose I need to save System.out somewhere first

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

That will cause NullPointerException when there is a System.out.println() statement somewhere.
Mayank SAssociate Director - Product EngineeringCommented:
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 ) ;
yongsingAuthor Commented:
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?
Mick BarryJava DeveloperCommented:
> 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.
Mayank SAssociate Director - Product EngineeringCommented:
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).
yongsingAuthor Commented:
>> 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?
Mayank SAssociate Director - Product EngineeringCommented:
>> But I still need to delete the file once I'm done.

I don't think that is necessary in File.createTempFile (). Even if it is, you can set it right there:

File f = File.createTempFile ( .... ) ;
f.deleteOnExit () ; // don't worry anymore
Mick BarryJava DeveloperCommented:
public class NullOutputStream extends OutputStream
{
  public void write (byte [] b) {}
  public void write (byte [] b, int off, int len) {}
  public void write (int b) {}
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
yongsingAuthor Commented:
>> f.deleteOnExit();

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

(Windows)
java X 2>&1 >nul

(*nix)

java X 2>&1 >/dev/null

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

;-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.