简体   繁体   English

write 和 readline 的意外行为(Java)

[英]Unexpected behavior with write and readline (Java)

I'm writing a client server program in which I have to send byte[] .我正在编写一个客户端服务器程序,我必须在其中发送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")我正在打印字节数组,通过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尝试使用InputStreamOutputStream

May be this question helps.可能这个问题有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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