簡體   English   中英

用FilterInputStream對象讀取大數據的一種優雅方法

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

我正在從函數中獲取FilterInputStream對象作為返回類型。 現在,我將作為流獲取的文件是日志文件。 因此,我認為這可能是個大文件。 因此,我不想一次讀取所有數據。 但是,循環讀取數據是一件乏味的工作。

我需要在每個換行符處進行拆分,這意味着文件中的數據采用行分隔格式。 public int read(byte[], int off, int len)使用恆定大小的字節數組public int read(byte[], int off, int len)因為它將引起很多情況。 我不想立即閱讀它,因為它可能很大。

有沒有一種優雅的方法可以做到這一點。

PS:我特別是指從FilterInputStream擴展的S3ObjectInputStream ,它具有read()函數。

BufferedReader包裹在FilterInputStreamInputStreamReader周圍,​​並調用readLine().

好的,很抱歉,下一層是對的,您可以使用BufferedReader類,它有一個名為readLine的方法,它返回String對象而不是字節數組。 像這樣

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