带有空行的缓冲读取器 readLine()
我正在使用缓冲阅读器从文本文件中一次抓取一行。我还尝试使用跟踪整数从文本文件中获取行号。不幸的是BufferedReader正在跳过空行(只有/n或回车符的空行)。
有更好的方法来解决这个问题吗?使用扫描仪有用吗?
示例代码:
int lineNumber = 0;
while ((s = br.readLine()) != null) {
this.charSequence.add(s, ++lineNumber);
}
I am using buffered reader to grab a line at a time from a text file. I am trying to also get the line number from the text file using a tracking integer. Unfortunately BufferedReader is skipping empty lines (ones with just /n or the carriage return).
Is there a better way to solve this? Would using scanner work?
Example code:
int lineNumber = 0;
while ((s = br.readLine()) != null) {
this.charSequence.add(s, ++lineNumber);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我无法重现您关于 BufferedReader 跳过空行的说法;它不应该有。
以下片段表明空行不仅仅是被跳过。
java.io.BufferedReader
java.io.LineNumberReader
java .util.Scanner
上述任何片段的输出是:
相关问题
LineNumberReader
+Scanner
组合!java 验证输入.util.Scanner
- 有很多示例%02d %01d
?I could not reproduce your claim that
BufferedReader
skips empty lines; it should NOT have.Here are snippets to show that empty lines aren't just skipped.
java.io.BufferedReader
java.io.LineNumberReader
java.util.Scanner
The output for any of the above snippets is:
Related questions
LineNumberReader
+Scanner
combo!java.util.Scanner
- has many examples%02d %01d
?你看过 LineNumberReader 吗?不确定这是否会对您有帮助。
http://download.oracle.com/javase/ 6/docs/api/java/io/LineNumberReader.html
Have you looked at LineNumberReader? Not sure if that will help you.
http://download.oracle.com/javase/6/docs/api/java/io/LineNumberReader.html
那么肯定是 FileReader 类跳过了换行符。
我再次检查了 readLine() 的结果,它不包含新行符号,因此它发生在两个类 FileReader 和 BufferedReader 之间>。
It must be the FileReader class that skips newline characters then.
I checked the results of
readLine()
again and it didn't include the new line symbol so it is happening between the two classes FileReader and BufferedReader.