Java IO流拷贝视频

package dh12_1111.IO.buffers.test;

import java.io.*;

/**
 * @Description TODO
 * @Author chenmin
 * @Version 1.0
 * @Date 2022/11/11 14:54
 * <p>
 * 文件拷贝
 * 1.选择使用哪一个流对象
 * 2.FileXxx 和  BufferedXxx 两者哪一个效率高
 */
public class TestCopyFile {

    public static void main(String[] args) {

        f1();  //FIleInputStream  字节数组1024拷贝  1671
        //f2();  //FIleInputStream  1B1B字节拷贝 没成功文件损害
         f3();   //Buffered字节数组1024拷贝成功 370

        //f4(); //BufferedFIleInputStream  1B1B字节拷贝 没成功文件损害
    }

    private static void f1() {
        long start = System.currentTimeMillis();
        try (
                //根据数据源创建字节输入流对象
                FileInputStream fis = new FileInputStream("E:\\002temp\\a\\001.mp4");
                //根据目的地创建字节输出流对象
                FileOutputStream fos = new FileOutputStream("E:\\002temp\\b\\001.mp4");
        ) {
            int len;
            byte[] bytes = new byte[1024];
            while ((len = fis.read(bytes)) != -1) {
                fos.write(bytes, 0, len);
            }
            System.out.println("字节数组1024拷贝成功");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("拷贝192MB视频" + (end - start));

    }

    private static void f2() {
        long start = System.currentTimeMillis();
        try (
                //根据数据源创建字节输入流对象
                FileInputStream fis = new FileInputStream("E:\\002temp\\a\\001.mp4");
                //根据目的地创建字节输出流对象
                FileOutputStream fos = new FileOutputStream("E:\\002temp\\b\\001.mp4");

        ) {

		int by;  //一次读一个字节
		while((by = fis.read())!=-1) {
		fos.write(by);
		}

            System.out.println("一个字节一个字节成功");

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("拷贝192MB视频" + (end - start));
    }


    private static void f3() {
        long start = System.currentTimeMillis();
        try (
                //根据数据源创建字节输入流对象
                //根据目的地创建字节输出流对象
               FileInputStream fis = new FileInputStream("E:\\002temp\\a\\001.mp4");
               FileOutputStream fos = new FileOutputStream("E:\\002temp\\b\\001.mp4");

                BufferedInputStream bis = new BufferedInputStream(fis);
                BufferedOutputStream bos = new BufferedOutputStream(fos)

        ) {
            int len;
            byte[] bytes = new byte[1024];
            while ((len = bis.read(bytes)) != -1) {
                bos.write(bytes, 0, len);
            }
            System.out.println("Buffered字节数组1024拷贝成功");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("拷贝192MB视频" + (end - start));

    }

    private static void f4() {
        long start = System.currentTimeMillis();
        try (
                //根据数据源创建字节输入流对象
                //根据目的地创建字节输出流对象
                FileInputStream fis = new FileInputStream("E:\\002temp\\a\\001.mp4");
                FileOutputStream fos = new FileOutputStream("E:\\002temp\\b\\001.mp4");

                BufferedInputStream bis = new BufferedInputStream(fis);
                BufferedOutputStream bos = new BufferedOutputStream(fos)

        ) {
            int by;  //一次读一个字节
            while((by = fis.read())!=-1) {
                bos.write(by);
            }
            System.out.println("Buffered字节数组1024拷贝成功");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("拷贝192MB视频" + (end - start));

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值