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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM