Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding System.out.print calls of a class

Tags:

java

I'm using a java library (jar file). The author of the file put in a bunch of System.out.print and System.out.printlns. Is there any way to hide these messages for a particular object?

*EDIT: It looks like the jar file seems to be creating a bunch of threads and each thread has it's own System.out.println's...

like image 665
K2xL Avatar asked Dec 02 '11 22:12

K2xL


People also ask

Does system out Println print to the console?

System. out. println does not print to the console, it prints to the standard output stream ( System. out is Java's name of the standard output stream).

What is system out print called in Java?

Java System. out. println() is used to print an argument that is passed to it.

Why do we write system out in Java?

It is used when you want the result in two separate lines. It is called with "out" object. If we want the result in two separate lines, then we should use the println() method. It is also an overloaded method of PrintStream class.

What is the output system out Println?

The println() method of this accepts any a value ( of any Java valid type), prints it and terminates the line. By default, console (screen) is the standard output Stream (System.in) in Java and, whenever we pass any String value to System. out. prinln() method, it prints the given String on the console.


2 Answers

Change original PrintStream with a Dummy one which does nothing on it's write() method.

Don't forget to replace original PrintStream when you finished.

System.out.println("NOW YOU CAN SEE ME");

PrintStream originalStream = System.out;

PrintStream dummyStream = new PrintStream(new OutputStream(){
    public void write(int b) {
        // NO-OP
    }
});

System.setOut(dummyStream);
System.out.println("NOW YOU CAN NOT");

System.setOut(originalStream);
System.out.println("NOW YOU CAN SEE ME AGAIN");
like image 104
Kerem Baydoğan Avatar answered Sep 20 '22 18:09

Kerem Baydoğan


System.setOut(); is probably what you're looking for

System.setOut(new PrintStream(new OutputStream() {
  public void write(int b) {
    // NO-OP
  }
}));

Source

like image 39
Grambot Avatar answered Sep 21 '22 18:09

Grambot