码迷,mamicode.com
首页 > 其他好文 > 详细

字节流复制文件和字节缓冲流复制文件的时间比较

时间:2019-03-26 19:33:18      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:mp4   bis   esc   time   style   imp   input   throw   import   

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();
    }

}

 

字节流复制文件和字节缓冲流复制文件的时间比较

标签:mp4   bis   esc   time   style   imp   input   throw   import   

原文地址:https://www.cnblogs.com/qurui1998/p/10602793.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!