Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to redirect an OutputStream  RSS feed

 
Gaurav Chikara
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to write to an outputstream which is not a fileoutputstream but printoutputstream
Is there any way that from one class I write to this output stream and pass it to another class
(output stream is made like PrintStream ps = new PrintStream(system.out,true))
and from other class I can use this Stream to write it to somewhere say some file or some editor
Can any one pls suggest a crack for it
Thankx in advance
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a bit confused on what you are asking. Are you wanting to take what you are printing to the standard output and write that to a file, also?
When you write to a stream, especially with autoflush on, the data doesn't hang around to be reused. So let's say you use your PrintStream to write "Hello World!" (which will go to System.out), you couldn't pass that stream somewhere else and print "Hello World!" again from the same stream.
Also, file writers need to specify a file to write to, which is why you don't see any file streams being constructed from other streams. They are the "root" of your streams, encapsulated by other more usable streams. Clear as mud?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure I got your requirements straight. Would this help?

Can't swear that will compile but I've been using ByteArray input and output to redirect input and output for unit testing a class that thinks it is talking to sockets.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remembered another approach - implement or extend the required stream in your own class. Here's one I use with my logger.

[ October 09, 2003: Message edited by: Stan James ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!