为什么要使用缓冲流?缓冲流对文件复制速度有什么影响?

通过不使用缓冲流代码与使用缓冲流代码来对比测试一波:

package person.xsc.praticeIII;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Copy3 {
    public static void main(String[] args) throws FileNotFoundException{
        // TODO Auto-generated method stub         
         String srcPath="C:\\Users\\你是小朱老师呀\\Desktop\\JAVA编程.DOC";
         String destPath="C:\\Users\\你是小朱老师呀\\Desktop\\XSC\\test4.DOCX";
         //1.造源文件与目标文件
         File srcFile = new File(srcPath);
         File destFile = new File(destPath);
         //2.造节点流
         FileInputStream fis = new FileInputStream((srcFile));
         FileOutputStream fos = new FileOutputStream(destFile);  
         //文件大小
         long dataSize=srcFile.length();
         System.out.println("文件大小:" + dataSize + " B");
         //不使用缓冲流去复制
         long start0 = System.currentTimeMillis();
         byte[] byte0 = new byte[1024];
         int temp0 = 0 ;    
         try{
                while((temp0=fis.read(byte0))!=-1){    // 开始拷贝
                    fos.write(byte0,0,temp0) ;    // 边读边写
                }
                System.out.println("不使用缓冲流拷贝完成!") ;
            }catch(IOException e){
                e.printStackTrace() ;
                System.out.println("不使用缓冲流拷贝失败!") ;
            }
            try{
                fis.close() ;        // 关闭
                fos.close() ;        // 关闭
            }catch(IOException e){
                e.printStackTrace() ;
        }    
          long end0 = System.currentTimeMillis();
          //FileUtils.sizeOf(localFileCache)
          System.out.println("不使用缓冲流复制需要 " + (end0-start0) + " ms"+"复制速度为:" + dataSize / (end0-start0) + " B/ms");
          //使用缓冲流去复制
          BufferedInputStream bis = new BufferedInputStream(new FileInputStream((srcFile)));
          BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
          long start1 = System.currentTimeMillis();
          byte [] byte1 = new byte[1024];
          int temp1 = 0 ;    
          try{
                 while((temp1=bis.read(byte1))!=-1){    
                     bos.write(byte1,0,temp1) ;
                 }
                 System.out.println("使用缓冲流拷贝完成!") ;
             }catch(IOException e){
                 e.printStackTrace() ;
                 System.out.println("使用缓冲流拷贝失败!") ;
             }
             try{
                    fis.close() ;        // 关闭
                    fos.close() ;        // 关闭
                }catch(IOException e){
                    e.printStackTrace() ;
            }
           long end1 = System.currentTimeMillis();
           System.out.println("使用缓冲流复制需要 " + (end1-start1) + " ms"+"复制速度为:" + dataSize / (end1-start1) + " B/ms");
    }

}
输出:
文件大小:405504 B
不使用缓冲流拷贝完成!
不使用缓冲流复制需要 8 ms复制速度为:50688 B/ms
使用缓冲流拷贝完成!
使用缓冲流复制需要 2 ms复制速度为:202752 B/ms

通过上面程序运行结果发现:加入缓冲处理流的复制速度将有明显的提升。至于为什么复制速度会提升,是因为不带缓冲的复制操作,每读一个字节就要写入一个字节,由于涉及磁盘的IO操作相比内存的操作要慢很多,所以不带缓冲的流效率很低。带缓冲的流,可以一次读很多字节,但不向磁盘中写入,只是先放到内存里。等凑够了缓冲区大小的时候一次性写入磁盘,这种方式可以减少磁盘操作次数,速度就会提高很多!

评论

暂无评论