How to override (not the OOP override) the output of System.out.print()?
- create your own
PrintStream
- e.g.public class YourPrintStream extends PrinterStream
. - override the
print(String s)
method and change the string there any way you like. Then callsuper.print(s))
; - Call
System.setOut(new YourPrintStream())
Then everytime System.out.println
is called, the passed string will be under your control before going into the actual stream.
System.out
is an output stream. Once you put something in, you can't go in after it and change it. Even if you could, it is just a byte representation at that point. The closest you're going to come is making the modification before you send the data to standard out, or wrapping the console such that you capture the data on the other side.