欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

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

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

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

FileChannel屬于nio,F(xiàn)ileChannel底層會(huì)利用操作系統(tǒng)的零拷貝進(jìn)行優(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時(shí)

代碼

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();
        }

    }

說(shuō)明

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

sourceChannel.transferTo(0,sourceChannel.size(),destChannel);
參數(shù)說(shuō)明
0:表示從源文件的什么位置開始拷貝。
sourceChannel.size():拷貝多大。
destChannel:拷貝到那里去。
該方法的返回值為真實(shí)拷貝的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
//超過(guò) 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所拷貝過(guò)去的真實(shí)長(zhǎng)度
                //size - left計(jì)算出下次要拷貝的位置
                long transferSize = sourceChannel.transferTo((size - left), left, destChannel);
                //還剩余多少
                left=left-transferSize;
            }

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

    }

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

//超過(guò) 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海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧

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

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

廣告

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

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