zoukankan      html  css  js  c++  java
  • BufferedInputStream和BufferedOutputStream的使用实例

    package io;
    
    import org.junit.Test;
    
    import java.io.*;
    
    public class BufferedStreamDemoTest {
        private String path0 = System.getProperty("user.dir") + "/src/main/resources/copy0.txt";
    
        private String path = System.getProperty("user.dir") + "/src/main/resources/d.txt";
        private File file = null;
        private BufferedInputStream bis = null;
        private BufferedOutputStream bos = null;
    
        @Test
        public void copyTxt() throws Exception{
            file = new File(path0);
            bis = new BufferedInputStream(new FileInputStream("C:\Users\15082157\Desktop\自我介绍.txt"));
            bos = new BufferedOutputStream(new FileOutputStream(file));
            if (!(file.exists())) {
                file.createNewFile();
                BufferedStreamCopy();
            } else {
                BufferedStreamCopy();
            }
        }
    
    
        @Test
        public void copyMp4() throws Exception {
            String path1 = System.getProperty("user.dir") + "/src/main/resources/copy.mp4";
            file = new File(path1);
            bis = new BufferedInputStream(new FileInputStream("C:\Users\15082157\Desktop\3-4视频19秒.mp4"));
            bos = new BufferedOutputStream(new FileOutputStream(file));
            if (!(file.exists())) {
                file.createNewFile();
                BufferedStreamCopy();
            } else {
                BufferedStreamCopy();
            }
        }
    
        public void BufferedStreamCopy() throws Exception {
            byte[] buffer = new byte[1024];  // 创建一个长度是1024的byte类型的数组,用于存储读取到的数据
            int byteRead = 0;
            long beginTime = getCurrentTime1();
            while ((byteRead = bis.read(buffer)) != -1) {
                bos.write(buffer, 0, byteRead);
                bos.flush();
            }
            long endTime = getCurrentTime1();
            float time = (endTime - beginTime) / 1000F;
            System.out.println("copy时间为" + time + "s");
            bis.close();
            bos.close();
        }
    
        public long getCurrentTime1() {
            long currentTime = System.currentTimeMillis();
            System.out.println(currentTime);
            return currentTime;
        }
    }
  • 相关阅读:
    UVALive 3938 一道被我WA了的线段树
    批量删除Zen Cart 无图片商品
    zencart加大数据表字段长度
    CSS字体中英文名称对照表
    zencart产品批量表上传后SEO三要素状态以及特价时间修改
    Linux 文件系统
    VMware Tools 安装步骤
    IDEA安装阿里规约插件
    IDEA 中无法使用 EL 表达式
    Redis主从复制
  • 原文地址:https://www.cnblogs.com/KevinFeng/p/12955333.html
Copyright © 2011-2022 走看看