1. 程式人生 > >使用NIO進行快速的檔案拷貝

使用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"); } }