Java input/output stream time to copy file comparison

Source: Internet
Author: User

Package Com.edu.xynu;import Java.io.bufferedinputstream;import Java.io.bufferedoutputstream;import java.io.File; Import Java.io.fileinputstream;import Java.io.fileoutputstream;import Java.io.ioexception;public class IOUnitCopy {/ /press bytes public static void Copybybyte (File srcfile,file destfile) throws Ioexception{fileinputstream fis=new FileInputStream (Srcfile); FileOutputStream fos=new FileOutputStream (destfile); int I;while ((I=fis.read ())!=-1) {fos.write (i);} Fis.close (); Fos.close ();} By byte array public static void Copybybytearray (File srcfile,file destfile) throws Ioexception{fileinputstream fis=new FileInputStream (Srcfile); FileOutputStream fos=new FileOutputStream (destfile); byte []buf=new byte[10*1024];int I;while (i=fis.read (buf, 0, buf.length))!=-1) {fos.write (buf, 0, i);} Fis.close (); Fos.close ();} BYTE buffer stream public static void Copybybuff (File srcfile,file destfile) throws Ioexception{bufferedinputstream bis=new Bufferedinputstream (New FileInputStream (srcfile)); Bufferedoutputstream bos=new BUFFEREDOUTPUtstream (New FileOutputStream (destfile)); int I;while ((I=bis.read ())!=-1) {bos.write (i);} Bos.flush (); Bis.close (); Bos.close ();} Byte array bulk read buffered output stream written to public static void Copybybuffarray (File srcfile,file destfile) throws Ioexception{fileinputstream bis =new FileInputStream (srcfile); Bufferedoutputstream bos=new Bufferedoutputstream (New FileOutputStream (DestFile)); byte [] buf=new Byte[10*1024];int Len;while ((Len=bis.read (buf,0,buf.length))!=-1) {bos.write (Buf,0,len);} Bos.flush (); Bis.close (); Bos.close ();}}

Package Com.edu.xynu;import Java.io.file;import Java.io.ioexception;public class Iounitscopytest {public static void Main (string[] args) {//TODO auto-generated method Stubtry {long start=system.currenttimemillis ();// Iounitcopy.copybybyte (New file ("C:\\1.mp3"), new file (//"C:\\2.mp3"));//90713ms//iounitcopy.copybybytearray (new File ("C:\\1.mp3"), new file (//"C:\\3.mp3")),//41ms//iounitcopy.copybybuff (New file ("C:\\1.mp3"), new file (//"c:\\4. MP3 ")//556ms//iounitcopy.copybybytearray (New file (" C:\\1.mp3 "), new file (//" C:\\5.mp3 ");//30mslong end= System.currenttimemillis (); System.out.println (End-start);} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}
The test file is

Java input/output stream time to copy file comparison

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.