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