my dog learned polymorphism
The moose likes I/O and Streams and the fly likes Read a PrintStream! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Read a PrintStream!" Watch "Read a PrintStream!" New topic

Read a PrintStream!

Daniel Akesson

Joined: Jun 26, 2004
Posts: 6
Ok, did I a search here but no real luck.

I have this class that as one of the parameters accepts a PrintStream (apparently, since the example uses System.out, which is a PrintStream obj).

Now, how do I read this PrintStream obj? The idea here is ofcource not to use System.out, but an object from where I can then examine the text that is written there and use that in some way, instead of just showing it in the console.

I have checked the API, but dont really find anything usefull. I bet there is a way, but I cant find it.

sander hautvast
Ranch Hand

Joined: Oct 18, 2002
Posts: 71
recheck api
Daniel Akesson

Joined: Jun 26, 2004
Posts: 6
Um, ok... but cant you give me some hints?
Joe Ess

Joined: Oct 29, 2001
Posts: 9189

You can't read from a PrintStream. PrintStreams are for performing output. The PrintStream may, however, wrap some other stream (i.e. PipedOutputStream,ByteArrayOutputStream) which you may obtain an InputStream from. You may have to fill in some details for us to give you constructive advice.

[How To Ask Questions On JavaRanch]
Daniel Akesson

Joined: Jun 26, 2004
Posts: 6
I think the main problem I did was that the OutObj must have the opposite InObj to work... So, I read same as is written. Anyway, I used ByteArrayOutputStream and ByteArrayInputStream(BOutStream.toByteArray()...) to read that.

Thanks anyway,
I agree. Here's the link:
subject: Read a PrintStream!
It's not a secret anymore!