[英]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
包裹在FilterInputStream
的InputStreamReader
周圍,並調用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.