Faster way of copying data in Java?

后端 未结 4 668
粉色の甜心
粉色の甜心 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:39

    Here is a link to an excellent post explaining how to use nio channels to make copies of streams. It introduces a helper method ChannelTools.fastChannelCopy that lets you copy streams like this:

    final InputStream input = new FileInputStream(inputFile);
    final OutputStream output = new FileOutputStream(outputFile);
    final ReadableByteChannel inputChannel = Channels.newChannel(input);
    final WriteableByteChannel outputChannel = Channels.newChannel(output);
    ChannelTools.fastChannelCopy(inputChannel, outputChannel);
    inputChannel.close();
    outputChannel.close()
    

提交回复
热议问题