Writer
objects (including PrintWriter
) are intended specifically for output of character data. It sounds like you want an OutputStream
instead of a Writer
here.
Where did your PrintWriter
come from? If it was created by wrapping some kind of OutputStream
with an OutputStreamWriter
and then wrapping that with a PrintWriter
, then you should just use the original write(byte[] b)
method from the original OutputStream
, rather than trying to use a Writer
.
If you want to mix character output and byte output, you may need to use String.getBytes()
. Check out this example:
OutputStream o = this.conn.getOutputStream(); // Based on your comment
String s = "Hello, world!";
byte[] b = ...; // These are the raw bytes that you want to write
o.write(s.getBytes("UTF-8"));
o.write(b);
(Of course, this will only work if the system that is reading your output understands that you are writing a mixture of characters and raw bytes and knows how to handle the mixed data that you are sending it.)