Comparison of time spent in copying files by Java input and output streams _java

Source: Internet
Author: User
Tags flush

Nonsense is not much said, the key code as described below:

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 {//per byte public static void Copybybyte (File srcfile,file destfile) throws ioexception{FileInput 
Stream 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 Fi 
Leinputstream (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 buffered stream public static void Copybybuff (File srcfile,file destfile) throws ioexception{Bufferedinputstream bis=new Buf Feredinputstream (NewFileInputStream (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 write 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 stub try {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.copy 
Bybytearray (New file ("C:\\1.mp3"), new file (//"C:\\5.mp3"));//30ms long End=system.currenttimemillis (); 
System.out.println (End-start); 
catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); } 
} 
}

The test file is

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.