gpt4 book ai didi

java - PrintWriter println(String s) 方法 :\r\n not supported?

转载 作者:太空宇宙 更新时间:2023-11-04 08:14:23 25 4
gpt4 key购买 nike

我正在创建一个多线程聊天客户端服务器应用程序。在服务器连接中,我使用 PrintWriter println(String s) 方法将响应写入客户端。

PrintWriter out;
String msg = in.readLine();
String response = "";
if (msg.startsWith("nick: ") {
response = protocol.authenticate(msg); //returns a String that says "welcome " + nick
//if there are messages pending for the author who logged in add them to the response String
response+="\r\n"+textmsg;
} else { ... }
out.println(response);

当我运行客户端时,它使用 BufferedReader readLine() 方法从服务器读取数据,我收到欢迎消息,但没有收到客户端的待处理消息,但如果我使用

response+=textmsg;

它有效,所以我认为这是因为我正在使用\r\n 但我仍然需要在这两条消息之间打印一个新行。我该怎么办?

接受答案后编辑:最后我选择使用OutputStream和InputStream,这样我就可以发送我想要的各种字符串,即使使用\r\n。

最佳答案

要么为每行输出调用一次println(所以两次),要么仅使用\n,而不使用\r。这是 Java 的标准换行符,而 \r\n 是 Windows 特定的行尾序列。当然,在客户端,您现在还必须调用 readLine 两次。无法调用一次 readLine 并获取两行。如果您需要自定义分隔符,则必须使用其他内容,而不是 \n

关于java - PrintWriter println(String s) 方法 :\r\n not supported?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10699776/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com