简体   繁体   English

在阅读 CSV 时,我应该更喜欢 FileReader 而不是 BufferedReader?

[英]Should I prefer FileReader to BufferedReader while reading CSV?

In a Spring Boot app, I am reading csv file data using OpenCSV and it is possible to use FileReader to BufferedReader with it.在 Spring Boot 应用程序中,我正在使用 OpenCSV 读取 csv 文件数据,并且可以使用 FileReader 到 BufferedReader。 However, when I compare both of them, I have a dilemma for the following point:但是,当我比较它们时,我对以下几点感到进退两难:

BufferedReader is faster than FileReader, but it uses much more memory. BufferedReader 比 FileReader 快,但它使用更多 memory。

As I am reading multiple data file (having hundreds of thousands records) in the same method (first I read data from one csv and then use the retrieved id fields to read the second csv), I think I shouldn't use BufferedReader for less memory usage.当我以相同的方法读取多个数据文件(有数十万条记录)时(首先我从一个 csv 读取数据,然后使用检索到的 id 字段读取第二个 csv),我认为我不应该使用BufferedReader for less memory 用法。 But I am really not sure what is the most proper way.但我真的不确定什么是最合适的方法。

So, in this situation, Should I prefer FileReader to BufferedReader?那么,在这种情况下,我应该更喜欢 FileReader 而不是 BufferedReader?

As far as i know the difference in size lies simply in the buffer size, which by default is 8k or 16k, so the difference is memory isn't huge;据我所知,大小的差异仅在于缓冲区大小,默认情况下为 8k 或 16k,因此 memory 的差异并不大; the most important thing is you remember to free the resources when you don' use them anymore, calling close(), also remember to do it incase of Exceptions最重要的是当你不再使用它们时记得释放资源,调用 close(),也记得在异常情况下这样做

Generally speaking, depends on your constraints.一般来说,取决于你的限制。 If performance is an issue, allocate more resources and go for the faster solution.如果性能有问题,请分配更多资源和 go 以获得更快的解决方案。 If memory is an issue, do the reverse.如果 memory 是一个问题,则执行相反的操作。

With BufferedReader you can also use the reader and int constructor to set buffer size, which suits your needs.使用BufferedReader ,您还可以使用 reader 和 int 构造函数来设置适合您需要的缓冲区大小。

BufferedReader reader = new BufferedReader(Reader, bufferSize);

Another general rule of thumb, don't do premature optimizations, be it memory or performance.另一个一般的经验法则是,不要过早地进行优化,无论是 memory 还是性能。 Strive for clean code, if a problem arises, use a profiler to identify the bottlenecks and then deal with them.力求干净的代码,如果出现问题,使用分析器找出瓶颈,然后进行处理。

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

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