字节流几种读写方式效率对比

读写效率比较

通过字节流复制一个短视频,4种复制方式运行时间对比

demo:

package com.itxs.demo01;

import java.io.*;

/**
 * @Classname : demo09
 * @Description : TODO 读写效率对比
 * @Author : lin_refuel@qq.com
 */
public class demo09 {
    public static void main(String[] args) throws IOException {
          //定义出原文件名字的字符串
        String str = "D:/JAVA/ce/lixin.mp4";
        //定义出复制后的文件名字字符串
        String scr = "D:/JAVA/ce/newLixin.mp4";
           //定义出起始时间
        long star = System.currentTimeMillis();
           //TODO copy1(str,scr); //一次读取一个字节 耗时17719ms
           //TODO copy2(str,scr);// 一次读取一个数组耗时37ms
           //TODO copy3(str,scr);//缓冲区一次读取一个字节耗时185ms
           //TODO copy4(str,scr);//缓冲区一次读取一个数组耗时21ms
          //定义出结束的时间
        long over = System.currentTimeMillis();
        //计算出时间
        System.out.println("耗时" + (over - star) + "ms");
    }

    /**
     * 缓冲区一次读取一个数组
     * @param str 源文件
     * @param scr 目标文件
     */
    private static void copy4(String str, String scr) throws IOException {
        //封装写入
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(scr));
        //封装读取
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(str));
        byte[] bytes = new byte[1024];
        int len;//存储数组里面有效字节长度
        while((len = bis.read(bytes))!= -1)
            bos.write(bytes,0,len);
        //关闭读取和写入
        bos.close();
        bis.close();
    }

    /**
     * 缓冲区一次读取一个字节
     * @param str 源文件
     * @param scr 目标文件
     */
    private static void copy3(String str, String scr) throws IOException {
        //封装读取
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(str));
        //封装写入
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(scr));
         int len;
         while((len = bis.read()) != -1)
             bos.write(len);
        //关闭封装和读取
        bis.close();
        bos.close();
    }

    /**
     * 一次读取一个字节数组
     * @param str 源文件
     * @param scr 目标文件
     */
    private static void copy2(String str, String scr) throws IOException {
        //读取
        FileInputStream fis = new FileInputStream(str);
        //写入
        FileOutputStream fos = new FileOutputStream(scr);
        //定义一个数组进行缓存
        byte[] bytes = new byte[1024];
        int len;//字节有效长度
        while((len = fis.read(bytes))!= -1)
               fos.write(bytes,0,len);
        //关闭读取和写入
        fis.close();
        fos.close();
    }

    /**
     * 读取文件一次读取一个字节后,复制出新的文件
     * @param str 目标文件
     * @param scr 原文件
     */
    private static void copy1(String str, String scr) throws IOException {
        //读取
        FileInputStream fis = new FileInputStream(str);
        //写入
        FileOutputStream fos = new FileOutputStream(scr);
        int len;
        while ((len = fis.read())!= -1)
            fos.write(len);
        //关闭读取和写入
        fis.close();
        fos.close();
    }
}
  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值