java 四種方式實現字元流檔案的拷貝對比

來源:互聯網
上載者:User

標籤:軟體   row   output   位元組流   rgs   long   leo   方式   TE   

將D:\\應用軟體\\vm.exe  拷貝到C:\\vm.exe   四種方法耗費時間對比  4>2>3>1

package Copy;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;/* * 檔案複製方式,位元組流,一個4個方式 * 1.位元組流讀寫單個位元組 * 2.位元組流讀寫位元組數組 * 3.位元組緩衝區讀寫單個位元組 * 4.位元組緩衝區讀寫位元組數組 */public class Copy {    public static void main(String[] args) throws IOException {        long s=System.currentTimeMillis();        copy_4(new File("E:\\應用軟體\\vm.exe"),new File("c:\\vm.exe"));        long e=System.currentTimeMillis();        System.out.println(e-s);    }    /*     * 方法:實現檔案複製     *     4.位元組流緩衝區流讀寫位元組數組     */    public static void copy_4(File src,File desc)throws IOException{        BufferedInputStream bis=new BufferedInputStream(new FileInputStream(src));        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(desc));        int len=0;        byte[] bytes=new byte[1024];        while((len=bis.read(bytes))!=-1){            bos.write(bytes,0,len);        }        bos.close();        bis.close();    }    /*     *    方法:實現檔案複製         *     3,位元組流緩衝區流讀寫單個位元組     */    public static void copy_3(File src,File desc)throws IOException{        BufferedInputStream bis=new BufferedInputStream(new FileInputStream(src));        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(desc));        int len=0;        while((len=bis.read())!=-1){            bos.write(len);        }        bos.close();        bis.close();    }        /*     * 方法:實現複製     *     2.位元組流讀寫位元組數組     */    public static void copy_2(File src,File desc)throws IOException{        FileInputStream fis=new FileInputStream(src);        FileOutputStream fos=new FileOutputStream(desc);        int len=0;        byte[] bytes=new byte[1024];        while((len=fis.read(bytes))!=-1){            fos.write(bytes,0,len);                    }        fos.close();        fis.close();    }    /*     * 方法:實現檔案複製     * 1.位元組流讀寫單個位元組     */    public static void copy_1(File src,File desc) throws IOException{        FileInputStream fis=new FileInputStream(src);        FileOutputStream fos=new FileOutputStream(desc);        int len=0;        while((len=fis.read())!=-1){            fos.write( len);                    }        fos.close();        fis.close();    }}

 

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.