IO流讀寫(複製)視頻檔案練習(java)

來源:互聯網
上載者:User

標籤:flush   cat   time   ring   測試   from   nbsp   end   io流   

package com.chen.io1;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;/** * IO練習,一個複製視頻的類 * 練習檔案為大小1,443,621 位元組的wmv視頻檔案 * @author Administrator * */public class CopyVideo {    /**     * 方法一     * 基礎複製方法     * 使用InputStream,OutputStream     * 測試結果:基礎複製花費時間:10367毫秒;     * @param fromFileName     * @param toFileName     */    public static void baseCopyMethod(String fromFileName,String toFileName) {        long startTime = System.currentTimeMillis();        InputStream in = null;        OutputStream out = null;        try {            in = new FileInputStream(fromFileName);            out = new FileOutputStream(toFileName);            int tempRead ;            while((tempRead = in.read())>-1) {                out.write(tempRead);                out.flush();            }        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }finally {            try {                if(in!=null)in.close();                if(out!=null)out.close();            } catch (IOException e) {                e.printStackTrace();            }        }        long endTime = System.currentTimeMillis();        long spendTime = endTime - startTime;        System.out.println("方法一,基礎複製花費時間:"+ spendTime + "毫秒;");    }        /**     * 方法二     * 使用InputStream,OutputStream,並增加位元組數組作為緩衝區,提升效率     * 方法二,增加緩衝後複製用時:121毫秒     * @param fromFileName     * @param toFileName     */    public static void baseCopyMethod2(String fromFileName,String toFileName) {        long startTime = System.currentTimeMillis();        InputStream in = null;        OutputStream out = null;                try {            in = new FileInputStream(fromFileName);            out = new FileOutputStream(toFileName);            byte[] buf = new byte[102];//緩衝區數組,減少硬碟的讀取操作            int tempRead ;            while((tempRead=in.read(buf))>-1) {                out.write(buf);                out.flush();            }        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }finally {            try {                if(in!=null)in.close();                if(out!=null)out.close();            } catch (IOException e) {                e.printStackTrace();            }        }        long endTime = System.currentTimeMillis();        long spendTime = endTime - startTime;        System.out.println("方法二,增加緩衝後複製用時:"+ spendTime + "毫秒");    }        /**     * 方法三     * 方法三,使用BufferedInputStream和BufferedOutputStream     * 方法三,使用BufferedInputStream和BufferedOutputStream後複製用時:8268毫秒     * @param fromFileName     * @param toFileName     */    public static void bufferedCopyMethod(String fromFileName,String toFileName ) {        long startTime = System.currentTimeMillis();        BufferedInputStream in = null;        BufferedOutputStream out = null;        try {            in = new BufferedInputStream(new FileInputStream(fromFileName));            out = new BufferedOutputStream(new FileOutputStream(toFileName));            int readContent;            while((readContent = in.read())!=-1) {                out.write(readContent);                out.flush();            }        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }finally {            try {                if(in!=null)in.close();                if(out!=null)out.close();            } catch (IOException e) {                e.printStackTrace();            }        }        long endTime = System.currentTimeMillis();        long spendTime = endTime - startTime;        System.out.println("方法三,使用BufferedInputStream和BufferedOutputStream後複製用時:"+ spendTime + "毫秒");    }        /**     * 方法四     * 方法四,在BufferedInputStream和BufferedOutputStream基礎上再次添加緩衝數組     * 方法四,BufferedInputStream和BufferedOutputStream基礎上添加緩衝數組後複製用時:16毫秒     * @param fromFileName     * @param toFileName     */    public static void bufferedCopyMethod2(String fromFileName,String toFileName ) {        long startTime = System.currentTimeMillis();        BufferedInputStream in = null;        BufferedOutputStream out = null;        try {            in = new BufferedInputStream(new FileInputStream(fromFileName));            out = new BufferedOutputStream(new FileOutputStream(toFileName));            int readContent ;            byte[] buf = new byte[1024];            while((readContent=in.read(buf))!=-1) {                out.write(buf);                out.flush();            }        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }finally {            try {                if(in!=null)in.close();                if(out!=null)out.close();            } catch (IOException e) {                e.printStackTrace();            }        }        long endTime = System.currentTimeMillis();        long spendTime = endTime - startTime;        System.out.println("方法四,BufferedInputStream和BufferedOutputStream基礎上添加緩衝數組後複製用時:"+ spendTime + "毫秒");    }            public static void main(String[] args) {        String fromFileName = "C:\\Users\\Administrator\\Desktop\\test.wmv";        String toFileName = "C:\\Users\\Administrator\\Desktop\\test1.wmv";        //baseCopyMethod(fromFileName,toFileName);        //baseCopyMethod2(fromFileName,toFileName);        //bufferedCopyMethod(fromFileName,toFileName);        bufferedCopyMethod(fromFileName,toFileName);    }}

 

IO流讀寫(複製)視頻檔案練習(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.