How do you determine the ideal buffer size when using FileInputStream?

后端 未结 9 1869
北海茫月
北海茫月 2020-11-22 17:01

I have a method that creates a MessageDigest (a hash) from a file, and I need to do this to a lot of files (>= 100,000). How big should I make the buffer used to read from t

9条回答
  •  清歌不尽
    2020-11-22 17:36

    Yes, it's probably dependent on various things - but I doubt it will make very much difference. I tend to opt for 16K or 32K as a good balance between memory usage and performance.

    Note that you should have a try/finally block in the code to make sure the stream is closed even if an exception is thrown.

提交回复
热议问题