Java four ways to achieve copy comparison of character stream files

Source: Internet
Author: User
Tags file copy

It takes time to compare d:\\ application software \\vm.exe to C:\\vm.exe four methods 4>2>3>1

 PackageCopy;ImportJava.io.BufferedInputStream;ImportJava.io.BufferedOutputStream;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException;/** File copy method, byte stream, one 4 way * 1. Byte stream reads and writes single bytes * 2. Byte stream reads and writes an array of bytes * 3. Byte buffers Read and write a single byte * 4. Byte buffers read and write byte arrays*/ Public classCopy { Public Static voidMain (string[] args)throwsIOException {Longs=System.currenttimemillis (); Copy_4 (NewFile ("e:\\ application software \\vm.exe"),NewFile ("C:\\vm.exe")); LongE=System.currenttimemillis (); System.out.println (e-s); }    /** Method: Implement file Copy * 4. Byte stream read and write bytes array*/     Public Static voidCopy_4 (File src,file desc)throwsioexception{Bufferedinputstream bis=NewBufferedinputstream (NewFileInputStream (SRC)); Bufferedoutputstream Bos=NewBufferedoutputstream (NewFileOutputStream (DESC)); intLen=0; byte[] bytes=New byte[1024];  while((Len=bis.read (bytes))!=-1) {bos.write (bytes,0, Len);        } bos.close ();    Bis.close (); }    /** Method: Implement file Copy * 3, byte stream buffer streams read/write single bytes*/     Public Static voidCopy_3 (File src,file desc)throwsioexception{Bufferedinputstream bis=NewBufferedinputstream (NewFileInputStream (SRC)); Bufferedoutputstream Bos=NewBufferedoutputstream (NewFileOutputStream (DESC)); intLen=0;  while((Len=bis.read ())!=-1) {bos.write (len);        } bos.close ();    Bis.close (); }        /** Method: Implement Replication * 2. Byte stream reading and writing bytes Array*/     Public Static voidCopy_2 (File src,file desc)throwsioexception{FileInputStream fis=NewFileInputStream (SRC); FileOutputStream Fos=NewFileOutputStream (DESC); intLen=0; byte[] bytes=New byte[1024];  while((Len=fis.read (bytes))!=-1) {fos.write (bytes,0, Len);        } fos.close ();    Fis.close (); }    /** Method: Implement file Copy * 1. Byte stream read and write single bytes*/     Public Static voidCopy_1 (File src,file desc)throwsioexception{FileInputStream fis=NewFileInputStream (SRC); FileOutputStream Fos=NewFileOutputStream (DESC); intLen=0;  while((Len=fis.read ())!=-1) {fos.write (len);        } fos.close ();    Fis.close (); }}

Java four ways to achieve copy comparison of character stream files

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.