从零双排java之缓冲流

 * 缓冲流 (高效流) 

 * 内部自带一个缓冲区(相当有自带一个字节数组) 

 * BufferedOutputStream(写文件) 缓冲字节输出流

 * BufferedInputStream(读文件) 缓冲字节输入流

用缓冲流输出

		FileOutputStream fos =new FileOutputStream("/Users/lanou/Desktop/Test/guifen.txt");
			//构建一个缓冲流
		BufferedOutputStream bos =new BufferedOutputStream(fos);
		//写
		bos.write("guifen".getBytes());
		bos.close();

用缓冲流输入

FileInputStream fis = new FileInputStream("/Users/lanou/Desktop/Test/guifen.txt");
		BufferedInputStream bis = new BufferedInputStream(fis);
		int len = -1;
		byte[] bts = new byte[1024];
		while ((len = bis.read(bts)) != -1) {
			System.out.print(new String(bts, 0, len));
		}
		bis.close();

使用缓冲流看是否读取更快

public class Demo09 {
	public static void main(String[] args) {
		Temp temp = new InputStream();
		temp.printTime();
		Temp copy = new Copy();
		copy.printTime();
	}
}
abstract class Temp {
	public void printTime() {
		long time1 = System.currentTimeMillis();
		input();
		long time2 = System.currentTimeMillis();
		long time = time2 - time1;
		System.out.println(time + "毫秒");
	}
	public abstract void input();
}
class Copy extends Temp {

	@Override
	public void input() {
		FileInputStream fis = null;
		FileOutputStream fos = null;
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {
			fis = new FileInputStream("/Users/lanou/Desktop/Test/java核心技术 卷1 基础知识 原书第9版 完整中文版 .pdf");
			fos = new FileOutputStream("/Users/lanou/Desktop/Test/王者荣耀核心技术 .pdf");
			bis = new BufferedInputStream(fis);
			bos = new BufferedOutputStream(fos);
			int len = -1;
			byte[] bts = new byte[1024];
			while ((len = bis.read(bts)) != -1) {
				bos.write(bts, 0, len);
			}
		} catch (FileNotFoundException e) {
			throw new RuntimeException("找不到文件");
		} catch (IOException e) {
			throw new RuntimeException("读取失败");
		} finally {
			if (bis != null) {
				try {
					bis.close();
				} catch (IOException e) {
					throw new RuntimeException("流关闭失败");
				} finally {
					if (bos != null) {
						try {
							bos.close();
						} catch (IOException e) {
							throw new RuntimeException("流关闭失败");
						}
					}
				}
			}
		}
	}
}
class InputStream extends Temp {
	//2355毫秒
	@Override
	public void input() {
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream("/Users/lanou/Desktop/Test/java核心技术 卷1 基础知识 原书第9版 完整中文版 .pdf");
			fos = new FileOutputStream("/Users/lanou/Desktop/Test/dnf核心技术 .pdf");
			int len = -1;
			byte[] bts = new byte[1024];
			while ((len = fis.read(bts)) != -1) {
				fos.write(bts, 0, len);
			}
		} catch (FileNotFoundException e) {
			throw new RuntimeException("找不到文件");
		} catch (IOException e) {
			throw new RuntimeException("文件读取失败");
		} finally {
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					throw new RuntimeException("流关闭失败");
				} finally {
					if (fos != null) {
						try {
							fos.close();
						} catch (IOException e) {
							throw new RuntimeException("流关闭失败");
						}
					}
				}
			}
		}
	}
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值