How to Use filechannel to copy files

Filechannel is the content in javanio. Javanio (newio) is actually an IO multiplexing model. I won’t elaborate on this. If you want to know something about it, you can baidu.
This paper mainly uses buffer and channel in Java NiO to copy files.
The following is the code for copying the file:

package ch03;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
@Slf4j
public class FileCopy {
    private FileInputStream inputStream = null;
    private FileOutputStream outputStream = null;
    private FileChannel inChannel = null;
    private FileChannel outChannel = null;
    private double startTime;
    public void Copy(String src, String dest){
        startTime = System.currentTimeMillis();
        CopyFile(src,dest);
        log.info("the total time is " + (System.currentTimeMillis() - startTime) + "ms" );
    }

    private void CopyFile(String src,String dest){
        try {
            inputStream = new FileInputStream(new File(src));
            outputStream = new FileOutputStream(new File(dest));
            inChannel = inputStream.getChannel();
            outChannel = outputStream.getChannel();
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            int readLength = -1;
            while (-1 != (readLength = inChannel.read(buffer))){
                buffer.flip();//Switching from read mode to write mode
                int outLength = 0;
                while (0 != (outLength = outChannel.write(buffer))){
                log.info("写入了 --->"+ outLength +"KB" );
                }
                buffer.clear();
            }
            outChannel.force(true);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                inputStream.close();
                outputStream.close();
                inChannel.close();
                outChannel.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Read More: