java - 阅读包含多个换行符的文本
问题描述
我有以下文字:
My name is Omer
//new line
I want to start my own personal project and achieve a lot with it
//new line
//new line
I also have problems that I encounter along the way but I know I will overcome them
//new line
Good
Bye
//new line
Bye
这就是上面的例子,我也想使用 BufferedReader。假设我的 BufferedReader 被称为 br。使用 br.readLine(),我可以从控制台读取行(我将输入每个字母,我也使用 InputStreamerReader)
例如,如果我这样做:
String line;
do {
line = br.readLine();
} while(line.lenght != 0)
输入新行后它将停止。
我怎样才能正确阅读该文本?(我认为这是我在这里提出的第一个问题,对于我可能犯的任何错误,我深表歉意)
解决方案
您可以使用:
String line;
while((line=br.readLine())!=null) {
// your code here..
}
注意:不要这样做do-while
,使用while
. 您的输入可能从一开始就为空,并且您不希望出现Null Pointer Exception。
推荐阅读
- python - 如何在 django rest 框架中设置不需要的序列化器字段?
- javascript - 如何汇总从数据库中检索到的下拉列表中选择的商品的价格
- python - 循环遍历生成列表的生成器
- java - 树莓派 3 上的 Apache Ignite
- python - opt.apply_gradients() 返回“尝试更新张量”错误
- excel - 需要能够在 Excel 中将十进制度 (DD) 转换为十进制度分 (DDM)
- java - 不同方案的两个实体之间的关系——Spring Boot Data JPA
- python - 会员显示个人资料图片不会显示
- spring - Liquibase 池设置弹簧靴
- modelica - 如何在 Modelica 中描述 dy/dx 的导数?