IO流文件复制缓冲效率测试

package demo;

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 FileCopyTest {
	public static void main(String[] args) {

		long startTime = System.currentTimeMillis();

		// 单字节
		func1();
		long func1Time = System.currentTimeMillis();
		System.out.print("func1:");
		System.out.println(func1Time - startTime);

		// 单字节,BUFFER缓冲
		func2();
		long func2Time = System.currentTimeMillis();
		System.out.print("func2:");
		System.out.println(func2Time - func1Time);

		// 字节数组
		func3();
		long func3Time = System.currentTimeMillis();
		System.out.print("func3:");
		System.out.println(func3Time - func2Time);

		// 字节数组,BUFFER缓冲
		func4();
		long func4Time = System.currentTimeMillis();
		System.out.print("func4:");
		System.out.println(func4Time - func3Time);

		
	}

	public static void func1() {

		File file1 = null;
		File file2 = null;
		FileInputStream in = null;
		FileOutputStream out = null;

		try {
			file1 = new File("C:\\Users\\yhd82\\Desktop\\YHDJL.jpg");
			file2 = new File("C:\\Users\\yhd82\\Desktop\\YHDJL-copy1.jpg");

			in = new FileInputStream(file1);
			out = new FileOutputStream(file2);
			int n = -1;

			while ((n = in.read()) != -1)
				out.write(n);
		} catch (IOException e) {
			System.out.println(e.toString());
		} finally {
			if (in != null)
				try {
					in.close();
				} catch (IOException e) {
					System.out.println(e);
				}
			if (out != null)
				try {
					out.close();
				} catch (IOException e) {
					System.out.println(e);
				}
		}

	}

	public static void func2() {

		File file1 = null;
		File file2 = null;
		BufferedInputStream in = null;
		BufferedOutputStream out = null;

		try {
			file1 = new File("C:\\Users\\yhd82\\Desktop\\YHDJL.jpg");
			file2 = new File("C:\\Users\\yhd82\\Desktop\\YHDJL-copy2.jpg");

			in = new BufferedInputStream(new FileInputStream(file1));
			out = new BufferedOutputStream(new FileOutputStream(file2));
			int n = -1;

			while ((n = in.read()) != -1)
				out.write(n);
		} catch (IOException e) {
			System.out.println(e.toString());
		} finally {
			if (in != null)
				try {
					in.close();
				} catch (IOException e) {
					System.out.println(e);
				}
			if (out != null)
				try {
					out.close();
				} catch (IOException e) {
					System.out.println(e);
				}
		}

	}

	public static void func3() {
		File file1 = null;
		File file2 = null;
		FileInputStream in = null;
		FileOutputStream out = null;
		byte[] b = new byte[1024];
		int n = -1;

		try {
			file1 = new File("C:\\Users\\yhd82\\Desktop\\YHDJL.jpg");
			file2 = new File("C:\\Users\\yhd82\\Desktop\\YHDJL-copy3.jpg");

			in = new FileInputStream(file1);
			out = new FileOutputStream(file2);

			while ((n = in.read(b)) != -1)
				out.write(b, 0, n);
		} catch (IOException e) {
			System.out.println(e.toString());
		} finally {
			if (in != null)
				try {
					in.close();
				} catch (IOException e) {
					System.out.println(e);
				}
			if (out != null)
				try {
					out.close();
				} catch (IOException e) {
					System.out.println(e);
				}
		}

	}
	public static void func4() {

		File file1 = null;
		File file2 = null;
		BufferedInputStream in = null;
		BufferedOutputStream out = null;
		byte[] b = new byte[1024];
		int n = -1;

		try {
			file1 = new File("C:\\Users\\yhd82\\Desktop\\YHDJL.jpg");
			file2 = new File("C:\\Users\\yhd82\\Desktop\\YHDJL-copy4.jpg");

			in = new BufferedInputStream(new FileInputStream(file1));
			out = new BufferedOutputStream(new FileOutputStream(file2));

			while ((n = in.read(b)) != -1)
				out.write(b, 0, n);
		} catch (IOException e) {
			System.out.println(e.toString());
		} finally {
			if (in != null)
				try {
					in.close();
				} catch (IOException e) {
					System.out.println(e);
				}
			if (out != null)
				try {
					out.close();
				} catch (IOException e) {
					System.out.println(e);
				}
		}

	}
}
/*
 * 测试结果:
 * 文件大小:9.57 MB (10,040,209 字节)
 * func1:130261 ms
 * func2:480 ms
 * func3:169 ms
 * func4:44 ms
 * 
 */


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值