在 Java 中一次读取两行文本文件的最佳方法是什么?

发布于 2024-07-11 17:32:22 字数 282 浏览 5 评论 0原文

BufferedReader in;

String line;
while ((line = in.readLine() != null) {
    processor.doStuffWith(line);
}

这就是我逐行处理文件的方式。 然而,在这种情况下,我想在每次迭代中向处理器发送行文本。 (我正在处理的文本文件基本上将一条记录存储在两行中,因此我每次都向处理器发送一条记录。)

在 Java 中执行此操作的最佳方法是什么?

BufferedReader in;

String line;
while ((line = in.readLine() != null) {
    processor.doStuffWith(line);
}

This is how I would process a file line-by-line. In this case, however, I want to send two lines of text to the processor in every iteration. (The text file I'm processing essentially stores one record on two lines, so I'm sending a single record to the processor each time.)

What's the best way of doing this in Java?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

怀念你的温柔 2024-07-18 17:32:22

为什么不只读两行呢?

BufferedReader in;
String line;
while ((line = in.readLine() != null) {
    processor.doStuffWith(line, in.readLine());
}

这假设您可以依赖输入文件中包含完整的 2 行数据集。

Why not just read two lines?

BufferedReader in;
String line;
while ((line = in.readLine() != null) {
    processor.doStuffWith(line, in.readLine());
}

This assumes that you can rely on having full 2-line data sets in your input file.

べ映画 2024-07-18 17:32:22
BufferedReader in;
String line1, line2;

while((line1 = in.readLine()) != null 
   && (line2 = in.readLine()) != null))
{
    processor.doStuffWith(line1, line2);
}

或者,如果您愿意,也可以将它们连接起来。

BufferedReader in;
String line1, line2;

while((line1 = in.readLine()) != null 
   && (line2 = in.readLine()) != null))
{
    processor.doStuffWith(line1, line2);
}

Or you could concatenate them if you wanted.

守不住的情 2024-07-18 17:32:22

我会重构代码,使其看起来像这样:

RecordReader recordReader;
Processor processor;

public void processRecords() {
    Record record;

    while ((record = recordReader.readRecord()) != null) {
        processor.processRecord(record);
    }
}

当然,在这种情况下,您必须以某种方式将正确的记录读取器注入到此类中,但这不应该是问题。

RecordReader 的一种实现可能如下所示:

class BufferedRecordReader implements RecordReader
{
    BufferedReader in = null;

    BufferedRecordReader(BufferedReader in)
    {
        this.in = in;
    }
    public Record readRecord()
    {
        String line = in.readLine();

        if (line == null) {
            return null;
        }

        Record r = new Record(line, in.readLine());

        return r;
    }
}

I would refactor code to look somehow like this:

RecordReader recordReader;
Processor processor;

public void processRecords() {
    Record record;

    while ((record = recordReader.readRecord()) != null) {
        processor.processRecord(record);
    }
}

Of course in that case you have to somehow inject correct record reader in to this class but that should not be a problem.

One implementation of the RecordReader could look like this:

class BufferedRecordReader implements RecordReader
{
    BufferedReader in = null;

    BufferedRecordReader(BufferedReader in)
    {
        this.in = in;
    }
    public Record readRecord()
    {
        String line = in.readLine();

        if (line == null) {
            return null;
        }

        Record r = new Record(line, in.readLine());

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