字节流复制文件和字节缓冲流复制文件的时间比较
Posted qurui1998
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了字节流复制文件和字节缓冲流复制文件的时间比较相关的知识,希望对你有一定的参考价值。
package cn.lijun.demo2; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; public class Copy { public static void main(String[] args) throws Exception { long start=System.currentTimeMillis(); copy2(new File("d:\\d.mp4"),new File("c:\\d.mp4")); long end=System.currentTimeMillis(); System.out.println(end-start); } //字节流读取单个字节 public static void copy1(File src,File desc) throws Exception{ FileInputStream fis=new FileInputStream(src); FileOutputStream fos=new FileOutputStream(desc); int len=0; while((len=fis.read())!=-1){ fos.write(len); } fos.close(); fis.close(); } //字节流读写字节数组 5451 public static void copy2(File src,File desc) throws Exception{ FileInputStream fis=new FileInputStream(src); FileOutputStream fos=new FileOutputStream(desc); byte[] b=new byte[1024]; int len=0; while((len=fis.read(b))!=-1){ fos.write(b,0,len); } fos.close(); fis.close(); } //字节流缓冲区 读写单个字节 21208ms public static void copy3(File src,File desc) throws Exception{ BufferedInputStream bis=new BufferedInputStream(new FileInputStream(src)); BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(desc)); int len=0; while((len=bis.read())!=-1){ bos.write(len); } bos.close(); bis.close(); } //字节流缓冲区读写字节数组 5415ms public static void copy4(File src,File desc) throws Exception{ BufferedInputStream bis=new BufferedInputStream(new FileInputStream(src)); BufferedOutputStream bop=new BufferedOutputStream(new FileOutputStream(desc)); int len=0; byte[] b=new byte[1024]; while ((len=bis.read(b))!=-1){ bop.write(b,0,len); } bop.close(); bis.close(); } }
以上是关于字节流复制文件和字节缓冲流复制文件的时间比较的主要内容,如果未能解决你的问题,请参考以下文章