简体   繁体   中英

Unexpected behavior with write and readline (Java)

I'm writing a client server program in which I have to send byte[] . So, I'm using following code:

byte[] b = {}; //byte array of size 10
w2.write(new String(b, "utf-8") + "\n");     //exceptions etc. are handled

On the receiving side:

String s = r.readLine();
byte[] g = s.getBytes("utf-8");
//Now, when I print the string here, the o/p seems 
//to be the same as on the sending side but g.length is now 14 here.

I'm printing the byte array converting it to string by new String(byte array[], "utf-8")

It prints some unreadable characters too, but they match (So I don't think there is the problem)

What can the reason be for this kind of behavior?

This is how reader and writer are declared:

c = (SSLSocket) f.createSocket("127.0.0.1", 24910);
w = new BufferedWriter(new OutputStreamWriter(c.getOutputStream()));
r = new BufferedReader(new InputStreamReader(c.getInputStream()));

Try using InputStream and OutputStream

May be this question helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM