写入和读取行的意外行为 (Java)

Unexpected behavior with write and readline (Java)

我正在编写一个客户端服务器程序,我必须在其中发送 byte[]。所以,我使用以下代码:

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

接收方:

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.

我正在打印字节数组,通过 new String(byte array[], "utf-8")

将其转换为字符串

它也打印了一些不可读的字符,但它们匹配(所以我认为没有问题)

这种行为的原因是什么?

这是 reader 和 writer 的声明方式:

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

尝试使用 InputStreamOutputStream

可能 this question 有帮助。