简体   繁体   English

用FilterInputStream对象读取大数据的一种优雅方法

[英]elegant way to read large data with FilterInputStream object

I am getting a FilterInputStream object as a return type from a function. 我正在从函数中获取FilterInputStream对象作为返回类型。 Now the file which I will be getting as an stream is a log file. 现在,我将作为流获取的文件是日志文件。 So I think it can be big file. 因此,我认为这可能是个大文件。 So I do not want to read the data all at once. 因此,我不想一次读取所有数据。 But reading data in a loop is kind of tedious job. 但是,循环读取数据是一件乏味的工作。

I need splitting at every newline, meaning data in file is in line separated format. 我需要在每个换行符处进行拆分,这意味着文件中的数据采用行分隔格式。 With a constant size byte array to be used in public int read(byte[], int off, int len) as it will give rise to many cases. public int read(byte[], int off, int len)使用恒定大小的字节数组public int read(byte[], int off, int len)因为它将引起很多情况。 I do not want to read it at once because it can be of large size. 我不想立即阅读它,因为它可能很大。

Is there an elegant way to do this. 有没有一种优雅的方法可以做到这一点。

PS: I am in particularly referring to S3ObjectInputStream extended from FilterInputStream which has read() function. PS:我特别是指从FilterInputStream扩展的S3ObjectInputStream ,它具有read()函数。

BufferedReader包裹在FilterInputStreamInputStreamReader周围,​​并调用readLine().

OK, I am sorry, the next floor is right, you can use BufferedReader class, it has a method called readLine,and it returns a String object instead of a byte array. 好的,很抱歉,下一层是对的,您可以使用BufferedReader类,它有一个名为readLine的方法,它返回String对象而不是字节数组。 Just like this 像这样

BufferedReader reader = new BufferedReader(new FileReader(new File("the file path")));
String date = reader.readLine();
if(!StringUtil.isBlank(date)){
    //reade the file line by line
    date = reader.readLine();
}
reader.close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM