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

发布于 2024-09-15 08:43:48 字数 292 浏览 6 评论 0原文

我正在使用缓冲阅读器从文本文件中一次抓取一行。我还尝试使用跟踪整数从文本文件中获取行号。不幸的是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笛声青案梦长安 2024-09-22 08:43:48

我无法重现您关于 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

    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

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

上述任何片段的输出是:

0001: line1
0002: 
0003: 
0004: line4

相关问题

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

    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

    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

    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

心是晴朗的。 2024-09-22 08:43:48

你看过 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

这个俗人 2024-09-22 08:43:48

那么肯定是 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 (...) {

}

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.

BufferedReader br = null;
String s = null;

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文