This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Read a PrintStream!" Watch "Read a PrintStream!" New topic
Author

Read a PrintStream!

Daniel Akesson
Greenhorn

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.

Thanks,
Ace
sander hautvast
Ranch Hand

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

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

Joined: Oct 29, 2001
Posts: 8713
    
    6

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.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Daniel Akesson
Greenhorn

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,
Daniel
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Read a PrintStream!
 
Similar Threads
io
Need to create a similar functionality like System.out.println
Doubt regrding toString()
Speeding up the use of system.out
Reader/Writer problems