Why is using BufferedInputStream to read a file byte by byte faster than using FileInputStream?

前端 未结 3 1234
挽巷
挽巷 2020-11-28 19:49

I was trying to read a file into an array by using FileInputStream, and an ~800KB file took about 3 seconds to read into memory. I then tried the same code except with the F

3条回答
  •  感情败类
    2020-11-28 20:04

    A BufferedInputStream wrapped around a FileInputStream, will request data from the FileInputStream in big chunks (512 bytes or so by default, I think.) Thus if you read 1000 characters one at a time, the FileInputStream will only have to go to the disk twice. This will be much faster!

提交回复
热议问题