好记忆力不如烂笔头5-JAVA快速文件拷贝

好记性不如烂笔头5-JAVA快速文件拷贝

    如果使用luncene或者hadoop等文件系统的话,有大量的索引文件需要分发,可以利用现成的分发工具,也可以自己写程序进行快速的文件拷贝;

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

package com.daily;

 

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.nio.channels.FileChannel;

 

/**

 *NIO进行快速的文件拷贝,maxCount=128M,复制351M文件,耗时11001(毫秒)

 *同样的文件,用(64 * 1024 *1024) - (32 * 1024) ,耗时18192(毫秒)

 *@author 范芳铭

 */

public class EasyFastCopy {

 

    publicvoid fileCopy(String in, String out) throws IOException {

        FileChannelinChannel = new FileInputStream(new File(in)).getChannel();

        FileChanneloutChannel = new FileOutputStream(new File(out)).getChannel();

        try{

            //如果内存大,这个值可以适当扩大;对大文件的复制有优势

            //intmaxCount = (128 * 1024 * 1024);

            //intmaxCount = (8 * 1024 * 1024);

             int maxCount = (64 * 1024 * 1024) - (32 *1024);  

            longsize = inChannel.size();

            longposition = 0;

            while(position < size) {

                position+= inChannel

                        .transferTo(position,maxCount, outChannel);

            }

        }finally {

            if(inChannel != null) {

                inChannel.close();

            }

            if(outChannel != null) {

                outChannel.close();

            }

        }

    }

 

    publicstatic void main(String[] args) throws Exception {

        longstart = System.currentTimeMillis();

        EasyFastCopycopy = new EasyFastCopy();

        copy.fileCopy("D:/ffm83/big.rar","D:/ffm83/temp/big.rar");

        longend = System.currentTimeMillis();

        System.out.println("快速文件拷贝耗时:" + (end - start) +"(毫秒)");

    }

 

}