使用NIO进行快速的文件拷贝

/**
 * try-with-resouce
 * 
 * @author rgy
 *
 */
public class NioCopyDemo {

    public void copy(String inStr, String outStr) {
        File in = new File(inStr);
        File out = new File(outStr);
        try 
        (
            FileChannel inChannel = new FileInputStream(in).getChannel();
            FileChannel outChannel = new FileOutputStream(out).getChannel();
        ) {
            //被转移的最大字节数
            int maxCount = (64 * 1024 * 1024) - (32 * 1024);
            long size = inChannel.size();
            //文件的传输中的位置开始
            long position = 0;
            while (position < size) {
                position += inChannel.transferTo(position, maxCount, outChannel);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        NioCopyDemo nio = new NioCopyDemo();
        nio.copy("F:/TDDownload/Java典型应用彻查卷.zip.td", "d:/Java典型应用彻查卷.zip.td");
    }
}

版权声明:本文为u013179958原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。