Recently the company's project to use the file copy, because of the large number of large files involved in the copy work, code performance issues are particularly important, so write the following examples of several file copy operations to do a comparison:
0. File copy test method
1 Public Static voidFileCopy (string source, string target,inttype) {2Date start =NewDate ();3File in =NULL;4File out =NULL;5FileInputStream FIS =NULL;6FileOutputStream fos =NULL;7 Try {8in =NewFile (source);9out =NewFile (target);Ten Switch(type) { One Case1: A Copyer1 (in, Out, FIS, FOS); - Break; - Case2: the Copyer2 (in, Out, FIS, FOS); - Break; - Case3: - Copyer3 (in, Out, FIS, FOS); + Break; - Case4: + Copyer4 (in, Out, FIS, FOS); A Break; at default: - Break; - } -}Catch(Exception e) { - e.printstacktrace (); -}finally { in if(FIS! =NULL) { - Try { to fis.close (); +}Catch(IOException e) { - e.printstacktrace (); the } * } $ if(Fos! =NULL) {Panax Notoginseng Try { - fos.close (); the}Catch(IOException e) { + e.printstacktrace (); A } the } +Date end =NewDate (); -System.out.println ("Method" +type+ "spents:" + (End.gettime ()-start.gettime ()) + "ms!"); $ } $}
Mode one: Read all data at once
1 /**2 * Read the contents of the file all at once3 */4@SuppressWarnings ("Resource")5 Public Static voidCopyer1 (File in,file out,fileinputstream fis,fileoutputstream Fos)throwsioexception{6FIS =NewFileInputStream (in);7 intSize =fis.available ();8 byte[] buffer =New byte[size];9 fis.read (buffer);TenFOS =NewFileOutputStream (out); One fos.write (buffer); A Fos.flush (); -}
Mode two: Every time a fixed byte of data is read into
1 /**2 * Read fixed bytes of data every time3 */4@SuppressWarnings ("Resource")5 Public Static voidCopyer2 (File in,file out,fileinputstream fis,fileoutputstream Fos)throwsioexception{6 7FIS =NewFileInputStream (in);8FOS =NewFileOutputStream (out);9 byte[] buffer =New byte[1024];Ten while(Fis.read (buffer)! =-1) { One fos.write (buffer); A } - Fos.flush (); -}
Method Three: Every time one row of data is read, the scene is suitable for parsing data by rows
1 /**2 * One row of data per reading3 */ 4@SuppressWarnings ("Resource")5 Public Static voidCopyer3 (File in,file out,fileinputstream fis,fileoutputstream Fos)throwsioexception{6 7FIS =NewFileInputStream (in);8FOS =NewFileOutputStream (out);9BufferedReader br =NewBufferedReader (NewInputStreamReader (FIS));TenString line =NULL; One while(line = Br.readline ())! =NULL) { A Fos.write (Line.getbytes ()); - } - Fos.flush (); the}
Way four: Every time read a character, ~_~, think all tired
1 /**2 * read one byte at a time3 */4@SuppressWarnings ("Resource")5 Public Static voidCopyer4 (File in,file out,fileinputstream fis,fileoutputstream Fos)throwsioexception{6 7FIS =NewFileInputStream (in);8FOS =NewFileOutputStream (out);9 inti = 0;Ten while((i = Fis.read ())! = 1) { One Fos.write (i); A } - Fos.flush (); - } the}
Last: Test with main function
1 Public Static void Main (string[] args) {2 String Source = "E:\\in.txt"; 3 String target = "E:\\out.txt"; 4 for (int i = 1; i < 5; i++) {5 fileCopy (source, target, i); 6 }7 }
Test file:
Operation Result:
Several javai/o-based file copy operation comparisons