senzacionale
senzacionale

Reputation: 20906

PrintWriter and byte[] problem

byte[] data = (byte[])opBinding.execute();
PrintWriter out = new PrintWriter(outputStream);    
out.println(data);
out.flush();
out.close(); 

but instead of text i get @84654. How can i add byte[] to PrintWriter? I need byte[] and not strinf becouse i have encoing problems with čćžšđ

Upvotes: 3

Views: 19232

Answers (5)

Sathyan
Sathyan

Reputation: 86

It worked for me.. when i used

PrintWriter out=new PrintWriter(System.out);

Also it converts the byte data to String using toString() method.. So it may be a reason for your encoding problem

Upvotes: -1

Jon Skeet
Jon Skeet

Reputation: 1499780

PrintWriter is meant for text data, not binary data.

It sounds like you should quite possibly be converting your byte[] to a String, and then writing that string out - assuming the PrintWriter you're writing to uses an encoding which supports the characters you're interested in.

You'll also need to know the encoding that the original text data has been encoded in for the byte[], in order to successfully convert to text to start with.

Upvotes: 4

Kal
Kal

Reputation: 24910

You can use the outputstream directly to write the bytes.

outputStream.write(byte[] b);

Upvotes: 14

Pratik
Pratik

Reputation: 30855

try this

byte[] data = (byte[])opBinding.execute();
PrintWriter out = new PrintWriter(outputStream);    
out.println(new String(data));
out.flush();
out.close(); 

Upvotes: 1

nfechner
nfechner

Reputation: 17525

The problem is, that your code calls (implicitly) data.toString() before returning the result to your println statement.

Upvotes: 1

Related Questions