BufferedReader 问题最近做socket碰到一个奇怪的问题BufferedReader is new BufferedReader((new InputS
BufferedReader 问题
最近做socket碰到一个奇怪的问题
BufferedReader is = new BufferedReader((new InputStreamReader(client.getInputStream())));
char[] mChar = new char[100];
is.read(mChar);
System.out.println(mChar);
可以输出socket input的的内容
但是直接用readLine竟然不行
BufferedReader is = new BufferedReader((new InputStreamReader(client.getInputStream())));
System.out.println(is.readLine());
不知道两者的区别在哪里
[解决办法]我的理解是readLine函数应该是一个阻塞函数,如果没有接收到\n字符的话,就会一直停留在这里,你发送方发出的数据中有换行符吗?
[解决办法]这样的问题看API就行了。
public String readLine()
throws IOException
Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.
Returns:
A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached
Throws:
IOException - If an I/O error occurs
See Also:
Files.readAllLines(java.nio.file.Path, java.nio.charset.Charset)
public int read(char[] cbuf)
throws IOException
Reads characters into an array. This method will block until some input is available, an I/O error occurs, or the end of the stream is reached.
Parameters:
cbuf - Destination buffer
Returns:
The number of characters read, or -1 if the end of the stream has been reached
Throws:
IOException - If an I/O error occurs