InputStream+OutputStream+单字节/字节数组与BufferedInputStream+BufferedOutputStream+单字节/字节数组文件拷贝速度对比Java第十六天二

本文对比了使用InputStream、OutputStream与使用BufferedInputStream、BufferedOutputStream进行文件拷贝的速度。测试结果显示,缓冲区的使用显著提高了文件拷贝效率。

对比字节输入、输出流与高效缓冲区的输入、输出流文件拷贝速度

由下面测试的结果我们可以得出结论:

InputStream+
OutputStream+
单个字节
InputStream+
OutputStream+
字节数组
BufferedInputStream+
BufferedOutputStream+
单个字节
BufferedInputStream+
BufferedOutputStream+
字节数组
6406毫秒10毫秒61毫秒4毫秒
public class CopyFileSpeedTest {

	/**
	 * 此次测试文件大小为2.43 MB (2,549,668 字节)
	 */
	public static void main(String[] args) {
		long startTime = System.currentTimeMillis();
		
//		byteStreamCopyFile();
//		arrayStreamCopyFile();
//		byteBufferedStreamCopyFile();
//		arrayBufferedStreamCopyFile();
		
		long endTime = System.currentTimeMillis();
		System.out.println("此方法运行时间为:"+(endTime - startTime) +"毫秒");
	}
	/**
	 * 使用字节输入、输出流单个字节拷贝文件
	 * 使用时间:6406毫秒
	 */
	public static void byteStreamCopyFile() {
		
		InputStream in = null;
		OutputStream out = null;
		
		try {
			in = new FileInputStream("Test.pdf");
			out = new FileOutputStream("AAA.pdf");
			int len = -1;
			while((len = in.read()) != -1) {
				out.write(len);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch(IOException e) {
			e.printStackTrace();
		}finally {
				try {
					if(out != null) {
					out.close();
					}
					if(in != null) {
						in.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}
	
	/**
	 * 使用字节输入、输出流与数组拷贝文件
	 * 使用时间:10毫秒
	 */
	public static void arrayStreamCopyFile() {
		InputStream in = null;
		OutputStream out = null;
		
		try {
			in = new FileInputStream("Test.pdf");
			out = new FileOutputStream("AAA.pdf");
			byte[] be = new byte[1024];
			int len = -1;
			while((len = in.read(be)) != -1) {
				out.write(be,0,len);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch(IOException e) {
			e.printStackTrace();
		}finally {
				try {
					if(out != null) {
					out.close();
					}
					if(in != null) {
						in.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}
	

	/**
	 * 使用字节高效缓冲区输入、输出流单个字节拷贝文件
	 * 使用时间:61毫秒
	 */
	public static void byteBufferedStreamCopyFile() {
		InputStream in = null;
		OutputStream out = null;
		try {
			in = new BufferedInputStream(new FileInputStream("Test.pdf"));
			out = new BufferedOutputStream(new FileOutputStream("AAA.pdf"));
			int len = -1;
			while((len = in.read()) != -1) {
				out.write(len);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch(IOException e) {
			e.printStackTrace();
		}finally {
				try {
					if(out != null) {
					out.close();
					}
					if(in != null) {
						in.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}
	
	/**
	 * 使用字节高效缓冲区输入、输出流与数组拷贝文件
	 * 使用时间: 4毫秒
	 */
	public static void arrayBufferedStreamCopyFile() {
		InputStream in = null;
		OutputStream out = null;
		
		try {
			in = new BufferedInputStream(new FileInputStream("Test.pdf"));
			out = new BufferedOutputStream(new FileOutputStream("AAA.pdf"));
			byte[] be = new byte[1024];
			int len = -1;
			while((len = in.read(be)) != -1) {
				out.write(be,0,len);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch(IOException e) {
			e.printStackTrace();
		}finally {
				try {
					if(out != null) {
					out.close();
					}
					if(in != null) {
						in.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值