测试缓冲流和节点流速度

发布于 2021-08-13  60 次阅读


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();
            }
        }
    }

}