关于BufferefReader.readLine()方法的理解
2024-10-16 23:32:47
有以下代码:
BufferedReader localReader = new BufferedReader(new InputStreamReader(System.in));
String msg=null;
System.out.println("out of while loop!");
while((msg=localReader.readLine())!=null){
System.out.println(msg)
if(msg.equals("bye")){
break;
} }
本来以为localReader.readLine()读取不到数据时会返回null,结果执行代码后不输入数据时一直停留在while循环体内等待,故readLine()方法是一个阻塞函数。Java 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.
表示该方法读取一行文本,当遇到换行符"\n",回车符"\r"或者回车符后面紧跟着换行符时,该行结束并返回。没有数据时,将会一直处于等待状态。因此在进行网络连接时,应该避免使用该方法。
最新文章
- 学习3ds max插件开发过程中的一些小结
- JFinal - scheduler 插件做定时任务
- JavaScript校验图片格式及大小
- 华硕电脑安装ubuntu出现问题及决方案
- My集合框架第二弹 二叉树的实现
- 重置mysql管理员密码
- phpStorm 快捷键收集以及配色方案
- C语言生产随机数的方法
- PowerShell 管道和对象成员
- Android的BUG(二) - SurfaceTexture中的野指针
- HDU 5933/思维
- mysql新建数据库时的collation选择(转)
- JavaScript高级内容:原型链、继承、执行上下文、作用域链、闭包
- Oracle 每天自动生成AWR报告
- 2017年国内常用的公共NTP服务
- 数据结构与算法(十):红黑树与TreeMap详细解析
- web进修之—Hibernate起步(1)(2)
- java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context 错误
- centos7如何查询已运行服务?
- Beta冲刺——第一天
热门文章
- 安装 neo4j 在 .../bin 目录下使用 ./neo4j 没反应 和 从csv 导入数据到neo4j
- Oracle VM VirtualBox做好虚拟硬盘后,如何进一步修改虚拟硬盘的大小
- js中定时器之一
- 1、JDK自带注解
- dubbo-admin 管理平台
- scrollLeft滚动(用animate替代)
- IE8兼容background-size
- Educational Codeforces Round 59
- [leetcode]438. Find All Anagrams in a String找出所有变位词
- Spring集成Redis使用注解