Faster way of copying data in Java?

后端 未结 4 667
粉色の甜心
粉色の甜心 2021-02-06 17:00

I have been given a task of copying data from a server. I am using BufferedInputStream and output stream to copy the data and I am doing it byte by byte. Even thoug

4条回答
  •  不思量自难忘°
    2021-02-06 17:38

    Reading/writing byte-by-byte is definitely going to be slow, even though the actual reading/writing is done by chunks of the buffer size. One way to speed it up is to read/write by blocks. Have a look at read(byte[] b, int off, int len) method of BufferedInputStream. However it probably won't give you enough of the improvement.

    What would be much better is to use nio package (new IO) to copy data using nio channels. Have a look at nio documentation for more info.

提交回复
热议问题