复制代码 1 package IODemo; 2 3 import java.io.BufferedInputStream; 4 import java.io.BufferedOutputStream; 5 import java.io.FileInputStream; 6 import java.io.FileNotFoundException; 7 import java.io.FileOutputStream; 8 import java.io.IOException; 9 import java.io.InputStream;10 import java.io.OutputStream;11 12 public class CopyPic {13 14 public static void main(String[] args) {15 long start = System.currentTimeMillis();16 copyForBuffered();17 long end = System.currentTimeMillis();18 System.out.println((end-start)+"毫秒----缓冲区");//取出系统时间,相减后得出拷贝所用的时间19 long start2 = System.currentTimeMillis();20 copyForChar();21 long end2 = System.currentTimeMillis();22 System.out.println((end2-start2)+"毫秒---数组");23 }24 static void copyForChar(){25 // TODO Auto-generated method stub26 InputStream read = null;27 OutputStream write = null;28 try {29 read = new FileInputStream("C:/Users/Administrator/Desktop/IO/temp/伊瓜因进球1516进球记录.mp4");30 write = new FileOutputStream("C:/Users/Administrator/Desktop/IO/temp/伊瓜因copy2.mp4");31 byte[] saveBytes = new byte[1024];32 int numOfSave = 0;33 while((numOfSave=read.read(saveBytes))!=-1){34 write.write(saveBytes, 0, numOfSave);35 write.flush();36 }37 } catch (FileNotFoundException e) {38 // TODO Auto-generated catch block39 e.printStackTrace();40 }catch(Exception e){41 e.printStackTrace();42 }finally{43 try{44 if(read!=null){45 read.close();46 }47 if(write!=null){48 write.close();49 }50 }catch(Exception e){51 e.printStackTrace();52 }53 54 55 }56 }57 static void copyForBuffered() {58 InputStream read=null;59 OutputStream write =null;60 BufferedInputStream Bread =null;61 BufferedOutputStream Bwrite=null;62 try {63 read = new FileInputStream("C:/Users/Administrator/Desktop/IO/temp/伊瓜因进球1516进球记录.mp4");64 write = new FileOutputStream("C:/Users/Administrator/Desktop/IO/temp/伊瓜因BCOPY.mp4");65 Bread = new BufferedInputStream(read);66 Bwrite = new BufferedOutputStream(write);67 int saveNum = 0;68 byte[] saveByte = new byte[1024];69 while((saveNum=Bread.read(saveByte))!=-1){70 Bwrite.write(saveByte,0,saveNum);71 Bwrite.flush();72 }73 74 } catch (FileNotFoundException e) {75 // TODO Auto-generated catch block76 e.printStackTrace();77 }catch(IOException e){78 e.printStackTrace();79 }finally{80 try{81 if(Bwrite!=null){82 Bwrite.close();83 }84 if(Bread!=null){85 Bread.close();86 }87 }catch(Exception e){88 e.printStackTrace();89 }90 91 }92 }93 }高效缓冲区快
JAVA学习笔记——高效缓冲区和数组方式拷贝文件效率
最新推荐文章于 2022-10-27 10:30:13 发布