java操作大文件复制

1.大文件的复制可以用Java nio中的channel-to-channel传输,Channel-to-channel传输是可以极其快速的,特别是在底层操作系统提供本地支持的时候。某些操作系统可以不必通过用户空间传递数据而进行直接的数据传输。对于大量的数据传输,这会是一个巨大的帮助。

   2.代码

package com.dingwang.File;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;

/**
 * 类FileToFile.java的实现描述:TODO 类实现描述 <br>
 * 将一个文件中的内容写到另一个文件
 * 
 * @author wangding_91@163.com 2016年2月5日 上午11:55:26
 */
public class FileToFile {

    private static final int DEFAULT_BUFFER = 3 * 1024;

    /**
     * 利用通道copy文件
     * 
     * @param source
     * @param target
     */
    public void transfer(File source, File target) {

        FileInputStream in = null;
        FileOutputStream out = null;
        if (!source.exists() || !source.isFile()) {
            throw new IllegalArgumentException("file not exsits!");
        }

        if (target.exists()) {
            target.delete();
        }

        try {
            target.createNewFile();
            in = new FileInputStream(source);
            out = new FileOutputStream(target);
            FileChannel inChannel = in.getChannel();
            WritableByteChannel outChannel = out.getChannel();
            inChannel.transferTo(0, inChannel.size(), outChannel);
            inChannel.close();
            outChannel.close();
            in.close();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 传统的输入输出流copy文件
     * 
     * @param source
     * @param target
     */
    public void transfer2(File source, File target) {
        InputStream in = null;
        OutputStream out = null;
        if (!source.exists() || !source.isFile()) {
            throw new IllegalArgumentException("file not exsits!");
        }

        if (target.exists()) {
            target.delete();
        }

        byte[] buffer = new byte[DEFAULT_BUFFER];
        int n = 0;

        try {
            target.createNewFile();
            in = new BufferedInputStream(new FileInputStream(source));
            out = new BufferedOutputStream(new FileOutputStream(target));
            while ((n = in.read(buffer)) != -1) {
                out.write(buffer, 0, n);
            }
            out.flush();
            in.close();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
package com.dingwang.file;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.junit.Test;

import com.dingwang.File.FileToFile;

/**
 * 类FileToFileTest.java的实现描述:TODO 类实现描述
 * 
 * @author wangding_91@163.com 2016年2月5日 下午12:05:58
 */
public class FileToFileTest {

    //    @Test
    public void FileToFile() {
        String date = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        String username = System.getProperty("user.name");
        FileToFile f = new FileToFile();
        File source = new File("E:\生产问题查询\policy_biz_id_2015.dat");
        String targetFileName = "E:\生产问题查询\target_" + username + "_" + date + ".dat";
        File target = new File(targetFileName);
        Long start = System.currentTimeMillis();
        f.transfer(source, target);
        System.out.println("耗时=" + ((System.currentTimeMillis() - start)) + "ms");
    }

    @Test
    public void transfer2() {
        String date = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        String username = System.getProperty("user.name");
        FileToFile f = new FileToFile();
        //E:\生产问题查询\policy_biz_id_2015.dat
        File source = new File("E:\生产问题查询\policy_biz_id_2015.dat");
        String targetFileName = "E:\生产问题查询\target_" + username + "_" + date + ".dat";
        File target = new File(targetFileName);
        Long start = System.currentTimeMillis();
        f.transfer2(source, target);
        System.out.println("耗时=" + ((System.currentTimeMillis() - start)) + "ms");
    }
}

比较了下,利用通道复制文件比传统方式的大概快1倍有余,对于cpu和内存的消耗也更低,测试的时候用的300M的文件;监控用的jvisualvm

原文地址:https://www.cnblogs.com/coprince/p/6594348.html