java操作大檔案複製

來源:互聯網
上載者:User

標籤:system   ati   user   測試的   記憶體   []   unit   測試   實現   

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 [email protected] 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 [email protected] 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

java操作大檔案複製

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.