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;
        }
    }
  • 相关阅读:
    POJ 1789:Truck History
    POJ 1258:Agri-Net Prim最小生成树模板题
    POJ 1837:Balance 天平DP。。。
    杭电1754--I Hate It(线段树)
    Poj3259--Wormholes(Spfa 判负环)
    杭电1068--Girls and Boys(二分图最大独立集)
    杭电1010--Tempter of the Bone(Dfs+剪枝)
    杭电2647--Reward(反向拓扑)
    杭电1083--Courses(二分图匹配)
    杭电2063--过山车(二分匹配)
  • 原文地址:https://www.cnblogs.com/KevinFeng/p/12955333.html
Copyright © 2011-2022 走看看