Week Four Java Learning notes (iv)

Source: Internet
Author: User

Use Bufferedinputstream and bufferedoutputstream to reduce the time to copy audio files and run the effect:
ImportJava.io.BufferedInputStream;ImportJava.io.BufferedOutputStream;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;Importorg.junit.Test; Public classCopyFile {@Test Public voidTestcopyfile () {LongStart =System.currenttimemillis (); String src= "C:\\users\\think\\desktop\\java Job 4\\a.mp3"; String dest= "C:\\users\\think\\desktop\\java Job 4\\b.mp3";        CopyFile (src,dest); LongEnd =System.currenttimemillis (); System.out.print ("Time Spent:" + (End-start)); }        Private voidCopyFile (String src, string dest) {//using buffered streams for replicationBufferedinputstream bis =NULL; Bufferedinputstream Bos=NULL; Try {        //1. Provide read-in write fileFile file1 =NewFile (SRC); File file2=NewFile (dest); //2. Create the corresponding node streamFileInputStream FIS =NewFileInputStream (FILE1); FileOutputStream Fos=NewFileOutputStream (file2); //3. Passing the created node stream object to the buffered constructorBufferedinputstream bis1 =NewBufferedinputstream (FIS); Bufferedoutputstream BOS1=NewBufferedoutputstream (FOS); //4. Specific operations to implement file replication        byte[] B =New byte[1024];//using arrays for delivery, with 1024 bytes passed        intLen;  while(len = Bis1.read (b))! =-1) {Bos1.write (b,0, Len); }    } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }finally{        //5. Close the stream        if(Bos! =NULL){           Try{bos.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        }        if(bis! =NULL){           Try{bis.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        }    }  }}

Week Four Java Learning notes (iv)

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.