在Java中一次读取两行文本文件的最佳方法是什么? [英] What is the best way to read a text file two lines at a time in Java?

查看:701
本文介绍了在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.)

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

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

推荐答案

为什么不读取两行?

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

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

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

这篇关于在Java中一次读取两行文本文件的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆