IO stream read/write (copy) video file Practice (Java)

Source: Internet
Author: User

 PackageCom.chen.io1;ImportJava.io.BufferedInputStream;ImportJava.io.BufferedOutputStream;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream;/*** IO exercise, a Copy video class * Practice file for size 1,443,621 bytes wmv Video file *@authorAdministrator **/ Public classCopyvideo {/*** Method One * Basic Copy method * using Inputstream,outputstream * Test Result: Base copy takes time: 10367 milliseconds; *@paramFromfilename *@paramTofilename*/     Public Static voidBasecopymethod (String fromfilename,string tofilename) {LongStartTime =System.currenttimemillis (); InputStream in=NULL; OutputStream out=NULL; Try{ in=NewFileInputStream (fromfilename); out=NewFileOutputStream (tofilename); intTempread;  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 (); }        }        LongEndTime =System.currenttimemillis (); LongSpendtime = EndTime-StartTime; System.out.println ("Method One, the underlying replication takes time:" + Spendtime + "milliseconds;"); }        /*** Method Two * Use Inputstream,outputstream, and increase byte array as buffer, improve efficiency * Method Two, increase cache after replication time: 121 milliseconds *@paramFromfilename *@paramTofilename*/     Public Static voidbaseCopyMethod2 (String fromfilename,string tofilename) {LongStartTime =System.currenttimemillis (); InputStream in=NULL; OutputStream out=NULL; Try{ in=NewFileInputStream (fromfilename); out=NewFileOutputStream (tofilename); byte[] buf =New byte[102];//buffer array to reduce the read operation of the hard disk            intTempread;  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 (); }        }        LongEndTime =System.currenttimemillis (); LongSpendtime = EndTime-StartTime; System.out.println ("Method Two, increase the cache post-replication time:" + Spendtime + "milliseconds"); }        /*** Method Three * method three, using Bufferedinputstream and Bufferedoutputstream * method three, using Bufferedinputstream and Bufferedoutputstream after complex System time: 8268 milliseconds *@paramFromfilename *@paramTofilename*/     Public Static voidBufferedcopymethod (String fromfilename,string tofilename) {LongStartTime =System.currenttimemillis (); Bufferedinputstream in=NULL; Bufferedoutputstream out=NULL; Try{ in=NewBufferedinputstream (NewFileInputStream (fromfilename)); out=NewBufferedoutputstream (NewFileOutputStream (tofilename)); intreadcontent;  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 (); }        }        LongEndTime =System.currenttimemillis (); LongSpendtime = EndTime-StartTime; System.out.println ("Method three, when copying using Bufferedinputstream and Bufferedoutputstream:" + spendtime + "milliseconds"); }        /*** Method Four * Method four, add cache array again on Bufferedinputstream and Bufferedoutputstream * Method Four, Bufferedinputstream and Bufferedoutput Stream based on adding cache array after copy time: 16 milliseconds *@paramFromfilename *@paramTofilename*/     Public Static voidbufferedCopyMethod2 (String fromfilename,string tofilename) {LongStartTime =System.currenttimemillis (); Bufferedinputstream in=NULL; Bufferedoutputstream out=NULL; Try{ in=NewBufferedinputstream (NewFileInputStream (fromfilename)); out=NewBufferedoutputstream (NewFileOutputStream (tofilename)); intreadcontent; 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 (); }        }        LongEndTime =System.currenttimemillis (); LongSpendtime = EndTime-StartTime; System.out.println ("Method four, Bufferedinputstream and Bufferedoutputstream based on the addition of the cache array after the copy time:" + Spendtime + "milliseconds"); }             Public Static voidMain (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 stream read/write (copy) video file Practice (Java)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.