【Java】socket发送带空格的字符串被自动换行问题的解决

String sendMsg = input.next(); // 如果输入了带空格的字符串,会被读出为换行

String sendMsg = input.nextLine(); // 使用nextLine()【解决方案】

这样在接收方 readLine()的时候到\n就会被正确读取。

原因:
next()方法在Eclipse中会把输入的空格自动解析为 \n 处理,算是一个坑吧。

对比:
next()
nextLine()

发布了320 篇原创文章 · 获赞 311 · 访问量 66万+

猜你喜欢

转载自blog.csdn.net/sinat_36184075/article/details/104956735