• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

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!
0
yongsing
Asked:
yongsing
  • 8
  • 5
  • 4
  • +1
2 Solutions
 
objectsCommented:
sure use setOut(System.out), setErr(System.err) to put it back.
0
 
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?
0
 
objectsCommented:
try pointing it to null, otherwise send it to /dev/null or similiar
apache commons has a NullOutputStream
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

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

That will cause NullPointerException when there is a System.out.println() statement somewhere.
0
 
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 ) ;
0
 
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?
0
 
objectsCommented:
> 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.
0
 
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).
0
 
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?
0
 
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
0
 
objectsCommented:
public class NullOutputStream extends OutputStream
{
  public void write (byte [] b) {}
  public void write (byte [] b, int off, int len) {}
  public void write (int b) {}
}
0
 
yongsingAuthor Commented:
>> f.deleteOnExit();

It doesn't seem to work. The file is still there when the program exits with System.exit(0);
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.
0
 
CEHJCommented:
All you need to do is start your program thus:

(Windows)
java X 2>&1 >nul

(*nix)

java X 2>&1 >/dev/null

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

;-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 8
  • 5
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now