Writing 100,000 lines in a csv in fastest way possible

六眼飞鱼酱① 提交于 2020-06-17 09:32:10

问题


I m trying to create a zip of 100 csv. I need to write 100,000 lines in a single csv in fastest way possible. I am using openCSV, ZipEntry method to write to csv.

Some Code used:

ZipEntry zipentry = new ZipEntry(filename);
zos.putNextEntry(entry);
CSVWriter writer = new CsvWriter(new OutputStreamWriter(zos));
writer.writeNext(entries); //entries is single line of csv

Currently its taking 1.5 secs to write a single csv and overall its taking around 120-140secs to create complete zip.

I have debugged the code, and observed that the other computations in the code is not taking time, but the write operation does takes time.

I have tried creating list of 100,000 lines and then writing one one file at a time instead if direct streaming. But still it takes same time.

Please suggest fastest method which takes less time. ;-(


回答1:


BufferedOutputStream improves IO performance by a large margin:

see Javadoc: BufferedOutputStream

By setting up such an output stream, an application can write bytes to the underlying output stream without necessarily causing a call to the underlying system for each byte written.

OutputStream out = new BufferedOutputStream(zos, 1<<16);
CSVWriter writer = new CsvWriter(new OutputStreamWriter(out);



回答2:


Try using less aggressive compression level for your ZipOutputStream such as 3:

public void setLevel(int level)



来源:https://stackoverflow.com/questions/62350539/writing-100-000-lines-in-a-csv-in-fastest-way-possible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!