I was doing my academic project and while building and testing i have put many println() statements.
But when I had to submit all prints should not be displayed.
Can i implement something like listener which will be invoked when System.out is tried to be executed and prevents it from displaying. I dont know how feasible this idea is but just want to know whether its possible or not. I know i could have used a log file or write into a file but again its just a thought came into my mind if I have to disable SOP how can i do it ..
thanks
use System.setOut function (and setErr) The following program will only print 1: (and not 2)
public static void main(String[] args) throws FileNotFoundException {
System.out.println("1");
System.setOut(new PrintStream(new OutputStream() {
@Override
public void write(int arg0) throws IOException {
// TODO Auto-generated method stub
}
}));
System.out.println("2");
}
The correct thing to do is to either use flags before printlns, or better yet, to use a Logger (there are many versions available).
It is, however, possible to reroute all System.out away. Search for "redirect system.out" and you will find plenty of examples.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With