简体   繁体   English

PrintWriter 与 BufferedWriter

[英]PrintWriter vs BufferedWriter

I'm trying to transfer strings from my server to my client and I'm trying to find an explanation why when I'm using PrintWriter in my server the client receives the string while when I'm using BufferedWriter the client doesn't receive the string.我正在尝试将字符串从我的服务器传输到我的客户端,我试图找到一个解释,为什么当我在我的服务器中使用PrintWriter ,客户端会收到字符串,而当我使用BufferedWriter ,客户端没有收到字符串。

In my client I have the next readers/writers:在我的客户中,我有下一个读者/作者:

out=new PrintWriter(s.getOutputStream());
in=new BufferedReader(new InputStreamReader(s.getInputStream()));

In my main I'm receiving data from server with the next call:在我的主要内容中,我正在通过下一次调用从服务器接收数据:

String sol=in.readLine();

In my server I'm sending data with the next call (os is an outputStream that I get in my function):在我的服务器中,我将在下一次调用中发送数据(os 是我在函数中获得的 outputStream):

PrintWriter out= new PrintWriter(os);
out.write("test");
out.flush();

While when I use BufferedWriter it doesn't send data to the client (or the client can't receive it?) "而当我使用BufferedWriter它不会向客户端发送数据(或者客户端无法接收数据?)”

BufferedWriter out = new BufferedWriter(new OutputStreamWriter(os));
out.append("test"); // tried also using out.write
out.flush();  

On my server side Bufferwriter doesn't add "\\n" to the end of the string while in my client side I'm trying to read a line with inputstream.在我的服务器端,Bufferwriter 不会在字符串的末尾添加“\\n”,而在我的客户端,我试图用输入流读取一行。 Printwriter adds "\\n" in the method println. Printwriter 在方法 println 中添加了“\\n”。 Thanks to @EdwinDalorzo for the help.感谢@EdwinDalorzo 的帮助。

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

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