Java使用FileChannel進行文件拷貝(提升拷貝效率)-創(chuàng)新互聯(lián)

Java使用FileChannel進行文件拷貝
  • 1.當(dāng)所拷貝的文件小于2G時
  • 2.所拷貝內(nèi)容大于2G

目前創(chuàng)新互聯(lián)建站已為近1000家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)頁空間、網(wǎng)站運營、企業(yè)網(wǎng)站設(shè)計、成安網(wǎng)站維護等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

FileChannel屬于nio,F(xiàn)ileChannel底層會利用操作系統(tǒng)的零拷貝進行優(yōu)化,效率較io高。

導(dǎo)包

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
1.當(dāng)所拷貝的文件小于2G時

代碼

public void channelCopy(String sourcePath,String destPath){try {FileChannel sourceChannel = new FileInputStream(sourcePath).getChannel();
            FileChannel destChannel = new FileOutputStream(destPath).getChannel();

            sourceChannel.transferTo(0,sourceChannel.size(),destChannel);
        } catch (IOException e) {e.printStackTrace();
        }

    }

說明

sourcePath:源地址,如E:\xx\yy\a.txt
destPath:目的地址,如D:\yy\a.txt

sourceChannel.transferTo(0,sourceChannel.size(),destChannel);
參數(shù)說明
0:表示從源文件的什么位置開始拷貝。
sourceChannel.size():拷貝多大。
destChannel:拷貝到那里去。
該方法的返回值為真實拷貝的size。該方法大拷貝2G,超出2G的部分將丟棄。

解決掉異常,以及流關(guān)閉的完整封裝

public void channelCopy(String sourcePath,String destPath){FileChannel sourceChannel=null;
        FileChannel destChannel=null;

        try {sourceChannel = new FileInputStream(sourcePath).getChannel();
            destChannel = new FileOutputStream(destPath).getChannel();

            sourceChannel.transferTo(0,sourceChannel.size(),destChannel);
        } catch (IOException e) {e.printStackTrace();
        }finally {try {if (sourceChannel!=null){sourceChannel.close();
                }
            } catch (IOException e) {e.printStackTrace();
            }

            try {if (destChannel!=null){destChannel.close();
                }
            } catch (IOException e) {e.printStackTrace();
            }
        }

    }
2.所拷貝內(nèi)容大于2G
//超過 2g 大小的文件傳輸
    public void channelCopy(String sourcePath,String destPath){try {FileChannel sourceChannel = new FileInputStream(sourcePath).getChannel();
            FileChannel destChannel = new FileOutputStream(destPath).getChannel();
            //所要拷貝的原文件大小
            long size=sourceChannel.size();
            for (long left=size;left>0;){//transferSize所拷貝過去的真實長度
                //size - left計算出下次要拷貝的位置
                long transferSize = sourceChannel.transferTo((size - left), left, destChannel);
                //還剩余多少
                left=left-transferSize;
            }

        } catch (IOException e) {e.printStackTrace();
        }

    }

解決掉異常,以及流關(guān)閉的完整封裝

//超過 2g 大小的文件傳輸
    public void channelCopy(String sourcePath,String destPath){FileChannel sourceChannel=null;
        FileChannel destChannel=null;
        try {sourceChannel = new FileInputStream(sourcePath).getChannel();
            destChannel = new FileOutputStream(destPath).getChannel();

            long size=sourceChannel.size();
            for (long left=size;left>0;){long transferSize = sourceChannel.transferTo((size - left), left, destChannel);
                left=left-transferSize;
            }
        } catch (IOException e) {e.printStackTrace();
        }finally {try {if (sourceChannel!=null){sourceChannel.close();
                }
            } catch (IOException e) {e.printStackTrace();
            }

            try {if (destChannel!=null){destChannel.close();
                }
            } catch (IOException e) {e.printStackTrace();
            }
        }
    }

你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧

新聞標(biāo)題:Java使用FileChannel進行文件拷貝(提升拷貝效率)-創(chuàng)新互聯(lián)
文章出自:http://muchs.cn/article24/ddsdce.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊靜態(tài)網(wǎng)站、定制開發(fā)、用戶體驗微信公眾號、營銷型網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司