2014-03-07 68 views
1

我有一个字符串,如:b\na\n a \n a\n\ta\n应该有6行,但下面的代码跳过最后一个空行。BufferedReader似乎缺少结尾换行符?

br = new BufferedReader(new FileReader(file)); 
String line; 
while ((line = br.readLine()) != null) { 
    System.err.println("b4 line: " + line); 
} 

使用扫描仪似乎工作,但什么是错误的bufferedReader?

while ((line = br.readLine()) != null) { 
    System.err.println("b4 line: " + line); 
} 
+0

以下全部3个答案绝对**错误**。它确实很重要,因为BufferedReader对等地处理两种情况:https://stackoverflow.com/questions/9922859/bufferedreader-readline-issue-detecting-end-of-file-and-empty-return-lines – Andrew

回答

0

有5条线,你有最后\n后的5倍\n并没有什么。

所以没有什么是错的。

0

我算五行。您的字符串以\ n结尾,以便作为最后一行。最后一行之后没有数据。

0

readLine()寻找<line_content>\n并返回<line_content>回来。如果你在例子中计算这个外观,你会意识到,它应该只返回五行。

+1

如果我需要考虑一条线,是否有可能做到这一点? –

+0

你能列出你期望找到的行吗? –

+0

“b”,“a”,“a”,“a”,“\ ta”,“” –