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

时间:2015-09-22 12:53:06

标签: java bufferedreader readline bufferedwriter

我正在编写一个客户端服务器程序,我必须在其中发送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")

将其转换为字符串

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

这种行为的原因是什么?

这就是读者和作者的宣言:

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

1 个答案:

答案 0 :(得分:1)

尝试使用InputStreamOutputStream

可能this question有帮助。