侧边栏壁纸
博主头像
小顺

一帆风顺 ⛵️⛵️⛵️

  • 累计撰写 64 篇文章
  • 累计创建 0 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

测试缓冲流和节点流速度

小顺
2021-08-17 / 0 评论 / 0 点赞 / 55 阅读 / 246 字
package com.apesblog.day_26;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Test6 {

    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        bufferTest("src/com/apesblog/day_26/01.zip", "src/com/apesblog/day_26/buffercopy.zip");
        long end = System.currentTimeMillis();
        System.out.println(end - start);
        start = System.currentTimeMillis();
        fileTest("src/com/apesblog/day_26/01.zip", "src/com/apesblog/day_26/filecopy.zip");
        end = System.currentTimeMillis();
        System.out.println(end - start);
    }

    public static void bufferTest(String sfile, String scopy) {
        BufferedInputStream bufferInputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        try {
            byte[] buffer = new byte[1024];
            int len;
            bufferInputStream = new BufferedInputStream(new FileInputStream(new File(sfile)));
            bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File(scopy)));
            while ((len = bufferInputStream.read(buffer)) != -1) {
                bufferedOutputStream.write(buffer, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bufferInputStream != null)
                    bufferInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (bufferedOutputStream != null)
                    bufferedOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void fileTest(String sfile, String scopy) {
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            byte[] buffer = new byte[1024];
            int len;
            fileInputStream = new FileInputStream(sfile);
            fileOutputStream = new FileOutputStream(scopy);
            while ((len = fileInputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileInputStream != null)
                    fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fileOutputStream != null)
                    fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
0

评论区