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;
        }
    }
  • 相关阅读:
    Hdu 5256 系列转换
    SQL在declare声明变量
    煤火车问题
    百度音乐接口-——这可以基于在线音乐播放器来完成
    Android DrawerLayout 抽屉
    NETSH WINSOCK RESET这个命令的意义和效果?
    SSH三作品的框架和流程
    Solr入门指南
    客房收费制度的具体配置
    基本调试命令
  • 原文地址:https://www.cnblogs.com/KevinFeng/p/12955333.html
Copyright © 2011-2022 走看看