Java 带空行的缓冲读取器 readLine()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3527108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:33:01  来源:igfitidea点击:

Buffered Reader readLine() with Empty lines

javatextfilebuffered

提问by Walt

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 BufferedReaderis skipping empty lines (ones with just /nor the carriage return).

我正在使用缓冲阅读器从文本文件中一次抓取一行。我还尝试使用跟踪整数从文本文件中获取行号。不幸的是BufferedReader正在跳过空行(只有/n或回车的那些)。

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);
}

回答by Jim Tough

Have you looked at LineNumberReader? Not sure if that will help you.

你看过 LineNumberReader 吗?不确定这是否对你有帮助。

http://download.oracle.com/javase/6/docs/api/java/io/LineNumberReader.html

http://download.oracle.com/javase/6/docs/api/java/io/LineNumberReader.html

回答by polygenelubricants

I could not reproduce your claim that BufferedReaderskips empty lines; it should NOT have.

我无法重现您BufferedReader跳过空行的声明;它不应该有。

Here are snippets to show that empty lines aren't just skipped.

以下是一些片段,表明不只是跳过空行。

java.io.BufferedReader

java.io.BufferedReader

    String text = "line1\n\n\nline4";
    BufferedReader br = new BufferedReader(new StringReader(text));
    String line;
    int lineNumber = 0;
    while ((line = br.readLine()) != null) {
        System.out.printf("%04d: %s%n", ++lineNumber, line);
    }

java.io.LineNumberReader

java.io.LineNumberReader

    String text = "line1\n\n\nline4";
    LineNumberReader lnr = new LineNumberReader(new StringReader(text));
    String line;
    while ((line = lnr.readLine()) != null) {
        System.out.printf("%04d: %s%n", lnr.getLineNumber(), line);
    }

java.util.Scanner

java.util.Scanner

    String text = "line1\n\n\nline4";
    Scanner sc = new Scanner(text);
    int lineNumber = 0;
    while (sc.hasNextLine()) {
        System.out.printf("%04d: %s%n", ++lineNumber, sc.nextLine());
    }

The output for any of the above snippets is:

上述任何片段的输出是:

0001: line1
0002: 
0003: 
0004: line4

Related questions

相关问题

回答by Walt

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 FileReaderand BufferedReader.

它必须是 FileReader 类,然后跳过换行符。
readLine()再次检查了结果,它没有包含换行符,所以它发生在FileReaderBufferedReader两个类之间。

BufferedReader br = null;
String s = null;

try {
    br = new BufferedReader(new FileReader(fileName));
    while ((s = br.readLine()) != null) {
        this.charSequence.add(s);
    }
} catch (...) {

}