• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to create PrintStream using PrintWriter  RSS feed

 
Krishna Rao
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I have an interesting problem.

I have two instances of PrintWriter (one for out and other for err) in a particular method.

In that method, I am loading one more class using class loader and executing a particular method (lets call this as LoadedClass ublic static void main(String[] args) method.

Now, whatever output generated by LoadedClass ublic static void main(String[] args) should ideally be written to out/err printwriters that are available in the method.

For this I want to set System.setOut(out), System.setErr(err). But System.setOut/Err method take only PrintStream objects but not PrintWriter.

Hence, is there any way to create PrintStream using exisitng PrintWriter instances(out/err), so that the output generated by LoadedClass ublic static void main(String[] args) goes to these destinations.

Kindly help. And it is very much appreciated.

Thanks very much,
-Krishna Rao
 
Darrin Cartwright
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Krishna,

Is there any reason you cannot replace the PrintWriters with PrintStreams in your method? The functionality of the two classes is very similar.

Darrin
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Darren's suggestion would be the first choice, if possible. Or better, fix the evil LoadedClass so it doesn't dump important info to System.out and err. Otherwise you're basically looking for the opposite of an OutputStreamWriter. Java doesn't seem to provide anything like this, but you can make one:

This uses a plain OutputStream rather than PrintStream for maximum flexiblilty. To use it in your program, just wrap with a PrintStream.


Note that WriterOutputStream will only work reliably if the platform default encoding is a single-byte encoding. Which it usually is. But if it's UTF-8 or UTF-16 or some more exotic encoding, you may have problems if write() contains any incomplete byte sequences. E.g. if you use write(byte) for the first byte of a two-byte character, or if the last byte in a byte[] array is the first of a two-byte character, then that byte will be lost. And the next byte written will also be misinterpreted as a result. That won't be a problem on most systems, but if it is, you may need to do something more complex. Maybe involving a java.nio.charset.CharsetDecoder, which has the ability to tell you when there are leftover bytes that haven't been encoded yet. Or maybe there's a simpler trick which I'm overlooking.

Again, it would really be preferable to either fix the LoadedClass so it doesn't use System.out, or alter the calling method to remove the need for PrintWriters. But if you can't, it's still possible to proceed, carefully.
[ May 25, 2007: Message edited by: Jim Yingst ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!