您的位置:首页 > 编程语言 > Java开发

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

2015-01-29 17:01 423 查看
    如果使用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) +"(毫秒)");
    }
 
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息