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)