Java学习笔记------BufferedLine类的readLine()方法 使用须知
来源:互联网 发布:杭州seo外包服务 编辑:程序博客网 时间:2024/05/21 09:09
readLine
public String readLine() throws IOException
- 读取一个文本行。通过下列字符之一即可认为某行已终止:换行 ('\n')、回车 ('\r') 或回车后直接跟着换行。
- 返回:
- 包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null
- 抛出:
IOException
- 如果发生 I/O 错误
摘要:
- 误以为readLine()是读取到没有数据时就返回null(因为其它read方法当读到没有数据时返回-1),而实际上readLine()是一个阻塞函数,当没有数据读取时,就一直会阻塞在那,而不是返回null;因为readLine()阻塞后,System.out.println(message)这句根本就不会执行到,所以在接收端就不会有东西输出。要想执行到System.out.println(message),一个办法是发送完数据后就关掉流,这样readLine()结束阻塞状态,而能够得到正确的结果,但显然不能传一行就关一次数据流;另外一个办法是把System.out.println(message)放到while循环体内就可以。
- readLine()只有在数据流发生异常或者另一端被close()掉时,才会返回null值。
- 如果不指定buffer大小,则readLine()使用的buffer有8192个字符。在达到buffer大小之前,只有遇到"/r"、"/n"、"/r/n"才会返回。
readLine()的实质(下面是从JDK源码摘出来的):
- String readLine(boolean ignoreLF) throws IOException {
- StringBuffer s = null;
- int startChar;
- synchronized (lock) {
- ensureOpen();
- boolean omitLF = ignoreLF || skipLF;
- bufferLoop:
- for (;;) {
- if (nextChar >= nChars)
- fill(); //在此读数据
- if (nextChar >= nChars) { /* EOF */
- if (s != null && s.length() > 0)
- return s.toString();
- else
- return null;
- }
- ......//其它
- }
- private void fill() throws IOException {
- ..../其它
- int n;
- do {
- n = in.read(cb, dst, cb.length - dst); //实质
- } while (n == 0);
- if (n > 0) {
- nChars = dst + n;
- nextChar = dst;
- }
- }
从上面看出,readLine()是调用了read(char[] cbuf, int off, int len) 来读取数据,后面再根据"/r"或"/n"来进行数据处理。
在Java I/O书上也说了:
public String readLine() throws IOException
This method returns a string that contains a line of text from a text file. /r, /n, and /r/n are assumed to be line breaks and are not included in the returned string. This method is often used when reading user input from System.in, since most platforms only send the user's input to the running program after the user has typed a full line (that is, hit the Return key).
readLine() has the same problem with line ends that DataInputStream's readLine() method has; that is,the potential to hang on a lone carriage return that ends the stream. This problem is especially acute on networked connections, where readLine() should never be used.
小结,使用readLine()一定要注意:
- 读入的数据要注意有/r或/n或/r/n
- 没有数据时会阻塞,在数据流异常或断开时才会返回null
- 使用socket之类的数据流时,要避免使用readLine(),以免为了等待一个换行/回车符而一直阻塞
- Java学习笔记------BufferedLine类的readLine()方法 使用须知
- 关于JAVA中Console.readLine()方法的使用!
- 使用BufferReader类的readLine()方法注意问题
- read(), readline(), readlines()方法的使用
- read(), readline(), readlines()方法的使用
- 使用readLine()方法遇到的坑
- 关于java中BufferedReader的read()及readLine()方法的使用注意
- 关于java中BufferedReader的read()及readLine()方法的使用注意
- 关于java中BufferedReader的read()及readLine()方法的使用心得
- [转]关于java中BufferedReader的read()及readLine()方法的使用心得
- java初学者学习须知
- readline库函数的使用
- readline库的使用
- readline的使用
- Java IO学习【7】BufferedReader的学习-readLine原理
- Java BufferedReader readLine方法i
- 【java编程】IO类之复写BufferedReader里的readLine方法(带行号)
- NSTimer的使用须知
- 结构体内嵌一级指针
- 【ASP.NET】——统计在线人数、历史访问人数
- [Java]如何避免啰嗦的代码风格
- 机房收费系统(一)——MSFlexGrid控件
- maven pom.xml配置文件出错原因及解决办法
- Java学习笔记------BufferedLine类的readLine()方法 使用须知
- HDU A Mathematical Curiosity
- 堆排序(初学者)
- 【物理白痴进化论,欢迎吐槽】键盘按键原理及应用
- Parcelable接口的使用
- poj 1089 Intervals(区间合并问题)
- 快速幂
- Topcoder SRM 663 Div2 Hard: CheeseRolling(状压DP)
- 解决 Jenkins 没有权限监听 Gerrit 的 ‘Stream Events’的问题