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;
        }
    }
  • 相关阅读:
    一类涉及矩阵范数的优化问题
    MATLAB小实例:读取Excel表格中多个Sheet的数据
    深度多视图子空间聚类
    具有协同训练的深度嵌入多视图聚类
    结构深层聚类网络
    一种数据选择偏差下的去相关聚类方法
    shell编程基础二
    shell编程基础一
    如何处理Teamcenter流程回退情况
    汽车行业数字化车间解决方案
  • 原文地址:https://www.cnblogs.com/KevinFeng/p/12955333.html
Copyright © 2011-2022 走看看