Java中InputStream(或CsvMapper)中的总行数 [英] Total number of rows in an InputStream (or CsvMapper) in Java

查看:697
本文介绍了Java中InputStream(或CsvMapper)中的总行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从InputStream或CsvMapper获得行数(行),而无需循环遍历并计数它们?

How can I get the number of lines(rows) from an InputStream or from a CsvMapper without looping through and counting them?

下面有一个从CSV文件创建的InputStream。

Below I have an InputStream created from a CSV file.

InputStream content = (... from a resource ...);
CsvMapper mapper = new CsvMapper();
mapper.enable(CsvParser.Feature.WRAP_AS_ARRAY);
MappingIterator<Object[]> it = mapper
        .reader(Object[].class)
        .readValues(content);

是否有可能做类似的事情

Is it possible to do something like

int totalRows = mapper.getTotalRows();

我想在循环中使用此数字来更新进度。

I would like to use this number in the loop to update progress.

while (it.hasNextValue()){
    //do stuff here

    updateProgressHere(currentRow, totalRows);
}

很明显,我可以遍历并计数一次。然后再次循环浏览并处理它们,同时更新进度。由于其中一些InputStreams非常庞大,因此效率低下且速度缓慢。

Obviously, I can loop through and count them once. Then loop through again and process them while updating progress. This is inefficient and slow as some of these InputStreams are huge.

推荐答案

除非您提前知道行数,否则不进行循环是不可能的。您必须完整地读取该文件才能知道其中有多少行,而 InputStream CsvMapper 有一种为您预读和抽象的方法(它们都是面向流的接口)。

Unless you know the row count ahead of time, it is not possible without looping. You have to read that file in its entirety to know how many lines are in it, and neither InputStream nor CsvMapper have a means of reading ahead and abstracting that for you (they are both stream oriented interfaces).

没有一个接口低于code> ObjectReader 可以在支持下运行,查询底层文件的大小(如果是文件)或到目前为止已读取的字节数。

None of the interfaces that ObjectReader can operate on support querying the underlying file size (if it's a file) or number of bytes read so far.

选项是创建您自己的自定义 InputStream ,该方法还提供用于获取到目前为止已读取的总大小和字节数的方法,例如如果它是从文件读取的,则可以公开底层的 File.length()并跟踪读取的字节数。这可能并不完全准确,特别是如果杰克逊缓冲得很远的话,但这至少可以使您有所收获。

One possible option is to create your own custom InputStream that also provides methods for grabbing the total size and number of bytes read so far, e.g. if it is reading from a file, it can expose the underlying File.length() and also track the number of bytes read. This may not be entirely accurate, especially if Jackson buffers far ahead, but it could get you something at least.

这篇关于Java中InputStream(或CsvMapper)中的总行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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