Comparison of several file copy operations based on JAVAI/O

Source: Internet
Author: User
Tags file copy

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

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.