Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Modify the output of a PrintStream before print

807605Jul 27 2007 — edited Jul 27 2007
Hi,

This is quite complicated and I have little to no clue how it can be done. Basically I am using a 3rd party class (which I do not have the source of) which has a method that as a parameter takes the PrintStream it is to use to display the output on the screen. Now I want to actually know what is going to be outputted before it is displayed on the screen so that I can change some special characters. Following I did a very simple example of how this works:
import java.io.PrintStream;
public class Test8 {
 public Test8(){
  // 1.
  v(System.out);
 }
    
 private void v(PrintStream out){
  // Uses PrintStream to display 'Hi <name>'
  out.println("Hi {0}");        
 }

 public static void main(String[] args) {
  new Test8();
 }
}
In the code example above you can see that the methods named �v� will use the PrintStream object I passed as a parameter to display �Hi {0}�. However I want to capture that output so that I can replace that {0} with a parameter; example name.

What I thought of is actually passing a PrintStream that rather then outputs on the screen it outputs in some Buffer or String, so that I can then get that String, replace the elements I want and then print it normally. The psuedo code would be something like this:
import java.io.PrintStream;
public class Test8 {
 public Test8(){
  // 1.
  v(myStreamObject);
  // make my modifications
  System.out.print(myNewString);
 }
    
 private void v(PrintStream out){
  // Uses PrintStream to display 'Hi <name>'
  out.println("Hi {0}");        
 }

 public static void main(String[] args) {
  new Test8();
 }
}
However I checked the API�s and so far still did not find anything!

Does anyone here have any idea on what I can do to manage this? I know my solution could be wrong. But unfortunately I have no other idea how this can be done.

Thanks for any suggestions,
Regards,
Sim085

ps: for the second code example I thought of extending the PrintStream object (trying to do that now) but not sure if I can do that.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 24 2007
Added on Jul 27 2007
3 comments
662 views