作者SST2000 (BMW 428i)
看板java
标题[问题]请问关於文件读取行的问题!!!
时间Fri Jan 24 03:02:00 2020
我有一个文件*.txt档里面内容是这样
In this below-sea-level basin, steady drought and record summer heat make
Death Valley a
land of extremes. Yet, each extreme has a striking contrast. Towering peaks
are frosted with
winter snow. Rare rainstorms bring vast fields of wildflowers. Lush oases
harbor tiny fish
and refuge for wildlife and humans. Despite its morbid name, a great
diversity of life survives
in Death Valley.
在程式码里面用
while(bufferedReader.readLine() != null)
{
System.out.println(bufferedReader.readLine());
}
结果只会显示第二行和第四行
land of extremes. Yet, each extreme has a striking contrast. Towering peaks
are frosted with
and refuge for wildlife and humans. Despite its morbid name, a great
diversity of life survives
但是如果用
String str = null;
while((str=bufferedReader.readLine()) != null)
{
System.out.println(str);
}
就可以显示文件全部的内容,但是请问这两种方式在逻辑上好像
没甚麽不一样????请问为什麽会有这样的状况?
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 61.228.249.225 (台湾)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/java/M.1579806122.A.288.html
1F:推 vavamos: 看一下你哪边有用到readLine方法 用debug模式查就知道了 01/24 03:18
2F:→ dennisxkimo: 例1:while读 print读 while读 print 读 所以只 01/24 05:49
3F:→ dennisxkimo: 有双数行印出 01/24 05:49
4F:→ dennisxkimo: 例2:while读进str print已读进str (loop) 01/24 05:51
5F:→ dennisxkimo: 就会每行印出 01/24 05:54