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)