How to override (not the OOP override) the output of System.out.print()?

  1. create your own PrintStream - e.g. public class YourPrintStream extends PrinterStream.
  2. override the print(String s) method and change the string there any way you like. Then call super.print(s));
  3. 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.

Tags:

Java