0

Say I have some method or operation ReturnType method(ArgType argument) in Java. I want to make it so that invoking method will not cause some message to be written to System.out, and afterwards the behaviour should be as before. My naive idea for something doing this would be to write the following code:

PrintStream systemout = System.out
System.setOut(new PrintStream(new ByteArrayOutputStream()))
method(arg)
System.setOut(systemout)

But it does not seem to work. Is there some straightforward way of accomplishing this?

mstaal
  • 590
  • 9
  • 25
  • Is this kinda what you are looking for? https://stackoverflow.com/questions/34172384/recreate-system-out-to-print-again-in-console-after-system-out-close – callyalater May 18 '20 at 19:45
  • 1
    If you're still seeing output after redirecting `System.out`, considering redirecting `System.err` too. – Kayaman May 18 '20 at 19:52
  • Bless you, @Kayaman! System.err was indeed the "sinner"! – mstaal May 18 '20 at 20:36

0 Answers0