为了账号安全,请及时绑定邮箱和手机立即绑定

IO流中复制粘贴哪种快

标签:
Java
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();
    }
}
点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消