Java Fundamentals Hardening IO Stream Note 30: Byte stream 4 ways to copy MP4 and test efficiency

Source: Internet
Author: User

1. Demand: e:\\ brother has a wife. mp4 copied to Copy.mp4 in the current project directory

byte stream Four ways of copying files :
• Basic byte stream read and write one bytes at a time
• Basic byte stream reads and writes a single array of bytes at a time
• High-efficiency byte stream read and write one bytes at a time
• High-efficiency byte stream reads and writes a single array of bytes at a time

2. code example:

1  Packagecn.itcast_06;2 3 ImportJava.io.BufferedInputStream;4 ImportJava.io.BufferedOutputStream;5 ImportJava.io.FileInputStream;6 ImportJava.io.FileOutputStream;7 Importjava.io.IOException;8 9 /*Ten * Demand: e:\\ brother has a wife. mp4 copied to Copy.mp4 in the current project directory One  *  A * Byte stream four ways to copy files: - * Basic byte stream reads and writes one bytes at a time: Total time:117235 milliseconds - * Basic byte stream reads and writes a single array of bytes: Total time:156 milliseconds the * High-efficiency byte stream reads and writes one byte at a time: Total time:1141 milliseconds - * High-efficient byte stream reads and writes an array of bytes: Total time: milliseconds -  */ -  Public classCopymp4demo { +      Public Static voidMain (string[] args)throwsIOException { -         LongStart =System.currenttimemillis (); +         //method1 ("e:\\ brother has a wife. mp4", "Copy1.mp4"); A         //method2 ("e:\\ brother has a wife. mp4", "Copy2.mp4"); at         //method3 ("e:\\ brother has a wife. mp4", "Copy3.mp4"); -METHOD4 ("e:\\ brother has a wife. mp4", "Copy4.mp4"); -         LongEnd =System.currenttimemillis (); -SYSTEM.OUT.PRINTLN ("Total Time:" + (End-start) + "millisecond"); -     } -  in     //high-efficiency byte stream reads and writes an array of bytes at a time: -      Public Static voidmethod4 (String srcstring, string deststring) to             throwsIOException { +Bufferedinputstream bis =NewBufferedinputstream (NewFileInputStream ( - srcstring)); theBufferedoutputstream BOS =NewBufferedoutputstream ( *                 NewFileOutputStream (deststring)); $ Panax Notoginseng         byte[] Bys =New byte[1024]; -         intLen = 0; the          while(len = Bis.read (bys))! =-1) { +Bos.write (bys, 0, Len); A         } the  + bos.close (); - bis.close (); $     } $  -     //an efficient byte stream reads and writes one bytes at a time: -      Public Static voidmethod3 (String srcstring, string deststring) the             throwsIOException { -Bufferedinputstream bis =NewBufferedinputstream (NewFileInputStream (Wuyi srcstring)); theBufferedoutputstream BOS =NewBufferedoutputstream ( -                 NewFileOutputStream (deststring)); Wu  -         intby = 0; About          while(by = Bis.read ())! =-1) { $ Bos.write (by); -  -         } -  A bos.close (); + bis.close (); the     } -  $     //basic byte stream reads and writes a single array of bytes at a time the      Public Static voidmethod2 (String srcstring, string deststring) the             throwsIOException { theFileInputStream FIS =NewFileInputStream (srcstring); theFileOutputStream fos =NewFileOutputStream (deststring); -  in         byte[] Bys =New byte[1024]; the         intLen = 0; the          while(len = Fis.read (bys))! =-1) { AboutFos.write (bys, 0, Len); the         } the  the fos.close (); + fis.close (); -     } the Bayi     //basic byte stream read and write one bytes at a time the      Public Static voidmethod1 (String srcstring, string deststring) the             throwsIOException { -FileInputStream FIS =NewFileInputStream (srcstring); -FileOutputStream fos =NewFileOutputStream (deststring); the  the         intby = 0; the          while(by = Fis.read ())! =-1) { the Fos.write (by); -         } the  the fos.close (); the fis.close ();94     } the}

Java Fundamentals Hardening IO Stream Note 30: Byte stream 4 ways to copy MP4 and test efficiency

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.