Is there a way I can override the System.out.print()
's output?
Here is what I am trying to do:
System.out.print("A B C D E F G H I J K L");
Will print : A B C D E F G H I J K L
Now I want to write a method which overrides this output from a specific position. Something like,
override(7, "X Y Z");
Should output
A B C X Y Z G H I J K L
These should happen right in the console.
PrintStream
- e.g. public class YourPrintStream extends PrinterStream
.print(String s)
method and change the string there any way you like. Then call super.print(s))
;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.
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