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