JAVA学习笔记——高效缓冲区和数组方式拷贝文件效率

复制代码 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 }高效缓冲区快
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值