继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

IO流中复制粘贴哪种快

慕仰6150269
关注TA
已关注
手记 1
粉丝 0
获赞 2
import java.io.*;
public class Test1 {
    public static void main(String[] args) throws IOException {
        File src=new File("C:\\Users\\Public\\Videos\\Sample Videos\\1.wmv");//25M视频
        File dest1=new File("C:\\Users\\Public\\Videos\\Sample Videos\\2.wmv");
        File dest2=new File("C:\\Users\\Public\\Videos\\Sample Videos\\3.wmv");
        File dest3=new File("C:\\Users\\Public\\Videos\\Sample Videos\\4.wmv");
        File dest4=new File("C:\\Users\\Public\\Videos\\Sample Videos\\5.wmv");

        Long start1=System.currentTimeMillis();
        copyByByte(src,dest1);
        Long end1=System.currentTimeMillis();
        System.out.println(end1-start1);//209091毫秒

        Long start2=System.currentTimeMillis();
        copyByBytes(src,dest2);
        Long end2=System.currentTimeMillis();
        System.out.println(end2-start2);//84毫秒

        Long start3=System.currentTimeMillis();
        copyByBuffered(src,dest3);
        Long end3=System.currentTimeMillis();
        System.out.println(end3-start3);//376毫秒

        Long start4=System.currentTimeMillis();
        copyByBufferedBytes(src,dest4);
        Long end4=System.currentTimeMillis();
        System.out.println(end4-start4);//71毫秒
    }

    //一个字节一个字节复制
    public static void copyByByte(File src,File dest) throws IOException{
        FileOutputStream fos=new FileOutputStream(dest);
        FileInputStream fis=new FileInputStream(src);

        int b;
        while((b=fis.read())!=-1){
            fos.write(b);
        }   
        fos.flush();

        fis.close();
        fos.close();
    }

    //批量复制
    public static void copyByBytes(File src,File dest) throws IOException{
        FileOutputStream fos=new FileOutputStream(dest);
        FileInputStream fis=new FileInputStream(src);

        byte[] bytes=new byte[10240];
        int b;
        while((b=fis.read(bytes, 0, bytes.length))!=-1){
            fos.write(bytes, 0, b);
        }   
        fos.flush();

        fis.close();
        fos.close();
    }

    //缓冲复制
    public static void copyByBuffered(File src,File dest) throws IOException{
        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(dest));
        BufferedInputStream bis=new BufferedInputStream(new FileInputStream(src));

        int b;
        while((b=bis.read())!=-1){
            bos.write(b);
        }   
        bos.flush();

        bis.close();
        bos.close();
    }

    //缓冲+批量复制
    public static void copyByBufferedBytes(File src,File dest) throws IOException{
        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(dest));
        BufferedInputStream bis=new BufferedInputStream(new FileInputStream(src));

        byte[] bytes=new byte[10240];
        int b;
        while((b=bis.read(bytes, 0, bytes.length))!=-1){
            bos.write(bytes, 0, b);
        }
        bos.flush();

        bis.close();
        bos.close();
    }
}
打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP